- 包
- 权限修饰符
- final
- 常量
- 常量概述和基本作用
- 常量做信息标志和分类
- 枚举
- 枚举的概述
- 枚举的使用场景演示
- 抽象类
- 抽象类概述
- 抽象类的案例
- 抽象类的特征、注意事项小结
- 抽象类的应用知识:模板方法模式
- 接口
- 接口概述、特点
- 接口的基本使用:被实现
- 接口与接口的关系:多继承
- JDK8开始接口新增方法
- 使用接口的注意事项【了解】
package com.itheima.d1_package.demo1;
public class Animal {
}
package com.itheima.d1_package.demo1;
public class Cat {
public void run(){
System.out.println("猫1跑的贼溜~~");
}
}
package com.itheima.d1_package.demo2;
public class Cat {
public void run(){
System.out.println("猫2跑的贼快~~");
}
}
package com.itheima.d1_package;
public class Student {
}
package com.itheima.d1_package;
import com.itheima.d1_package.demo1.Animal;
import com.itheima.d1_package.demo1.Cat;
public class Test {
public static void main(String[] args) {
// 导包:相同包下的类可以直接访问。
Student s = new Student();
// 不同包下的类必须导包才可以使用
Animal a = new Animal();
// 使用默认导包的类:demo1下的cat
Cat c1 = new Cat();
c1.run();
// 指定使用demo2下的Cat类
com.itheima.d1_package.demo2.Cat c2 = new com.itheima.d1_package.demo2.Cat();
c2.run();
}
}
权限修饰符
package com.itheima.d2_modifier.itcast;
public class Demo {
public static void main(String[] args) {
//创建Fu的对象,测试看有哪些方法可以使用
Fu f = new Fu();
// f.show1(); // 私有的
f.show2();
f.show3();
f.show4();
}
}
package com.itheima.d2_modifier.itcast;
public class Fu {
// 1.private 只能本类中访问
private void show1() {
System.out.println("private");
}
// 2.缺省:本类,同一个包下的类中。
void show2() {
System.out.println("缺省");
}
// 3.protected:本类,同一个包下的类中,其他包下的子类
protected void show3() {
System.out.println("protected");
}
// 4.任何地方都可以
public void show4() {
System.out.println("public");
}
public static void main(String[] args) {
//创建Fu的对象,测试看有哪些方法可以使用
Fu f = new Fu();
f.show1();
f.show2();
f.show3();
f.show4();
}
}
package com.itheima.d2_modifier.itheima;
import com.itheima.d2_modifier.itcast.Fu;
public class Demo {
public static void main(String[] args) {
//创建Fu的对象,测试看有哪些方法可以使用
Fu f = new Fu();
f.show4();
}
}
package com.itheima.d2_modifier.itheima;
import com.itheima.d2_modifier.itcast.Fu;
public class Zi extends Fu {
public static void main(String[] args) {
//创建Zi的对象,测试看有哪些方法可以使用
Zi z = new Zi();
z.show3();
z.show4();
}
}
final
package com.itheima.d3_final;
public class Test {
// 属于类,只加载一次,可以共享 (常量)
public static final String schoolName = "黑马";
public static final String schoolName2;
static{
schoolName2 = "传智";
// schoolName2 = "传智"; // 第二次赋值,报错了!
}
// 属于对象的! (final基本上不会用来修饰实例成员变量,没有意义!)
private final String name = "王麻子";
public static void main(String[] args) {
// final修饰变量,变量有且仅能被赋值一次。
final int age;
age = 12;
// age = 20; // 第二次赋值,报错了!
System.out.println(age);
final double rate = 3.14;
buy(0.8);
// schoolName = "传智"; // 第二次赋值,报错了!
Test t = new Test();
// t.name = "麻子"; // 第二次赋值,报错了!
System.out.println(t.name);
}
public static void buy(final double z){
// z = 0.1; // 第二次赋值,报错了!
}
}
//final class Animal{
//}
//class Cat extends Animal{
//}
class Animal{
public final void run(){
System.out.println("动物可以跑~~");
}
}
class Tiger extends Animal{
// @Override
// public void run() {
// System.out.println("老虎跑的贼快~~~");
// }
}
package com.itheima.d3_final;
public class Test2 {
public static void main(String[] args) {
// final修饰变量的注意事项:
// 1、final修饰基本类型变量,其数据不能再改变
final double rate = 3.14;
// rate = 3.15; // 第二次赋值,报错
// 2、final修饰引用数据类型的变量,变量中存储的地址不能被改变,但是地址指向的对象内容可以改变。
final int[] arr = {10, 20, 30};
System.out.println(arr);
// arr = null; // 属于第二次赋值,arr中的地址不能被改变
arr[1] = 200;
System.out.println(arr);
System.out.println(arr[1]);
}
}
常量
常量概述和基本作用
package com.itheima.d4_constant;
public class ConstantDemo1 {
public static final String SCHOOL_NAME = "传智集团";
public static final String USER_NAME = "itheima";
public static final String PASS_WORD = "123456";
public static void main(String[] args) {
System.out.println(SCHOOL_NAME);
System.out.println(SCHOOL_NAME);
System.out.println(SCHOOL_NAME);
System.out.println(SCHOOL_NAME);
System.out.println(SCHOOL_NAME);
System.out.println(SCHOOL_NAME);
if(USER_NAME.equals("")){
}
}
}
常量做信息标志和分类
package com.itheima.d4_constant;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class ConstantDemo2 {
public static final int UP = 1; // 上
public static final int DOWN = 2; // 上
public static final int LEFT = 3; // 左
public static final int RIGHT = 4; // 右
public static void main(String[] args) {
// 1、创建一个窗口对象(桌子)
Jframe win = new Jframe();
// 2、创建一个面板对象(桌布)
JPanel panel = new JPanel();
// 3、把桌布垫在桌子上
win.add(panel);
// 4、创建四个按钮对象
JButton btn1 = new JButton("上");
JButton btn2 = new JButton("下");
JButton btn3 = new JButton("左");
JButton btn4 = new JButton("右");
// 5、把按钮对象添加到桌布上去
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
// 6、显示窗口
win.setLocationRelativeTo(null);
win.setSize(300,400);
win.setVisible(true);
btn1.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(UP) ; // 让玛丽往上跳
}
});
btn2.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(ConstantDemo2.DOWN) ; // 让玛丽往下跳
}
});
btn3.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(LEFT) ; // 让玛丽往左跑
}
});
btn4.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(RIGHT) ; // 让玛丽往右跑
}
});
}
public static void move(int flag){
// 控制玛丽移动
switch (flag) {
case UP:
System.out.println("玛丽往上飞了一下~~");
break;
case DOWN:
System.out.println("玛丽往下蹲一下~~");
break;
case LEFT:
System.out.println("玛丽往左跑~~");
break;
case RIGHT:
System.out.println("玛丽往→跑~~");
break;
}
}
}
枚举
枚举的概述
package com.itheima.d5_enum;
public enum Season {
// 枚举的第一行必须罗列枚举类的对象名称,建议全部大写。
SPRING, SUMMER, AUTUMN, WINTER;
}
枚举的使用场景演示
package com.itheima.d5_enum;
public enum Orientation {
UP, DOWN, LEFT, RIGHT;
}
package com.itheima.d5_enum;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class EnumDemo2 {
public static void main(String[] args) {
// 1、创建一个窗口对象(桌子)
Jframe win = new Jframe();
// 2、创建一个面板对象(桌布)
JPanel panel = new JPanel();
// 3、把桌布垫在桌子上
win.add(panel);
// 4、创建四个按钮对象
JButton btn1 = new JButton("上");
JButton btn2 = new JButton("下");
JButton btn3 = new JButton("左");
JButton btn4 = new JButton("右");
// 5、把按钮对象添加到桌布上去
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
// 6、显示窗口
win.setLocationRelativeTo(null);
win.setSize(300,400);
win.setVisible(true);
btn1.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(Orientation.UP) ; // 让玛丽往上跳
}
});
btn2.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(Orientation.DOWN) ; // 让玛丽往下跳
}
});
btn3.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(Orientation.LEFT) ; // 让玛丽往左跑
}
});
btn4.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(Orientation.RIGHT) ; // 让玛丽往右跑
}
});
}
public static void move(Orientation o){
// 控制玛丽移动
switch (o) {
case UP:
System.out.println("玛丽往上飞了一下~~");
break;
case DOWN:
System.out.println("玛丽往下蹲一下~~");
break;
case LEFT:
System.out.println("玛丽往左跑~~");
break;
case RIGHT:
System.out.println("玛丽往→跑~~");
break;
}
}
}
抽象类
抽象类概述
package com.itheima.d6_abstract_class;
public abstract class Animal {
private String name;
public abstract void run();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.itheima.d6_abstract_class;
public class Tiger extends Animal{
@Override
public void run() {
System.out.println("老虎跑的贼溜~~~~");
}
}
package com.itheima.d6_abstract_class;
public class Dog extends Animal{
@Override
public void run() {
System.out.println("狗跑的也很快~~~");
}
}
package com.itheima.d6_abstract_class;
public class Test {
public static void main(String[] args) {
Tiger t = new Tiger();
t.run();
Dog t1 = new Dog();
t1.run();
}
}
package com.itheima.d7_abstract_test;
public abstract class Card {
private String name; // 主人名称
private double money;
public abstract void pay(double money);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
package com.itheima.d7_abstract_test;
public class GoldCard extends Card{
@Override
public void pay(double money) {
// 优惠后的金额算出来:
double rs = money * 0.8;
double lastMoney = getMoney() - rs;
System.out.println(getName() + "当前账户总金额:"
+ getMoney() +",当前消费了:" + rs +",消费后余额剩余:" +
lastMoney);
setMoney(lastMoney); // 更新账户对象余额
}
}
package com.itheima.d7_abstract_test;
public class Test {
public static void main(String[] args) {
GoldCard c = new GoldCard();
c.setMoney(10000); // 父类的
c.setName("三石");
c.pay(300);
System.out.println("余额:" + c.getMoney());
}
}
抽象类的特征、注意事项小结
package com.itheima.d8_abstract_attention;
public abstract class Animal {
private String name;
public Animal(){
}
public abstract void run();
public abstract void run2();
public void eat(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.itheima.d8_abstract_attention;
public class Test {
public static void main(String[] args) {
// 有得有失: 得到了抽象方法,失去了创建对象的能力。
// Animal a = new Animal();
// 抽象类为什么不能创建对象?
// a.run();
// 类有的成员(成员变量、方法、构造器)抽象类都具备
// 抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类
// 一个类继承了抽象类必须重写完抽象类的全部抽象方法,
// 否则这个类也必须定义成抽象类。
// 不能用abstract修饰变量、代码块、构造器。
}
}
class Cat extends Animal{
@Override
public void run() {
}
@Override
public void run2() {
}
}
抽象类的应用知识:模板方法模式
package com.itheima.d9_abstract_template.before;
public class CurrentAccount {
private String cardId;
private double money;
public CurrentAccount() {
}
public CurrentAccount(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public void handle(String loginName , String passWord ){
// a.判断登录是否成功
if("itheima".equals(loginName) && "123456".equals(passWord)){
System.out.println("登录成功。。");
// b.正式结算利息
double result = money * 0.0175; // 结算利息了
// c.输出利息详情
System.out.println("本账户利息是:"+ result);
}else{
System.out.println("用户名或者密码错误了");
}
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
package com.itheima.d9_abstract_template.before;
public class FixedAccount {
private String cardId;
private double money;
public FixedAccount() {
}
public FixedAccount(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public void handle(String loginName , String passWord ){
// a.判断登录是否成功
if("itheima".equals(loginName) && "123456".equals(passWord)){
System.out.println("登录成功。。");
// b.正式结算利息
double result = money * 0.035; // 结算利息了
if(money >= 100000){
result += money * 0.03;
}
// c.输出利息详情
System.out.println("本账户利息是:"+ result);
}else{
System.out.println("用户名或者密码错误了");
}
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
package com.itheima.d9_abstract_template.after;
public abstract class Account {
private String cardId;
private double money;
public Account() {
}
public Account(String cardId, double money) {
this.cardId = cardId;
this.money = money;
}
public final void handle(String loginName , String passWord ){
// a.判断登录是否成功
if("itheima".equals(loginName) && "123456".equals(passWord)){
System.out.println("登录成功。。");
// b.正式结算利息
// 当前模板方法知道所有子类账户都要结算利息,但是具体怎么结算,模板不清楚,交给具体的子类来计算
double result = calc();
// c.输出利息详情
System.out.println("本账户利息是:"+ result);
}else{
System.out.println("用户名或者密码错误了");
}
}
public abstract double calc();
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
package com.itheima.d9_abstract_template.after;
public class CurrentAccount extends Account {
public CurrentAccount(String cardId, double money) {
super(cardId, money);
}
@Override
public double calc() {
// b.正式结算利息
double result = getMoney() * 0.0175; // 结算利息了
return result;
}
}
package com.itheima.d9_abstract_template.after;
public class Test {
public static void main(String[] args) {
CurrentAccount acc = new CurrentAccount("ICBC-111", 100000);
acc.handle("itheima", "123456");
}
}
package com.itheima.d10_interface;
public interface SportManInterface {
// 接口中的成员:JDK 1.8之前只有常量 和 抽象方法
// public static final 可以省略不写,接口默认会为你加上!
// public static final String SCHOOL_NAME = "黑马";
String SCHOOL_NAME = "黑马";
// 2、抽象方法
// public abstract 可以省略不写,接口默认会为你加上!
// public abstract void run();
void run();
// public abstract void eat();
void eat();
}
package com.itheima.d10_interface;
public class Test {
public static void main(String[] args) {
// 接口不能创建对象!
// SportManInterface s = new SportManInterface();
}
}
接口的基本使用:被实现
package com.itheima.d11_interface_implements;
public interface Law {
void rule(); // 遵章守法
}
package com.itheima.d11_interface_implements;
public interface SportMan {
void run();
void competition();
}
package com.itheima.d11_interface_implements;
public class PingPongMan implements SportMan , Law{
private String name;
public PingPongMan(String name) {
this.name = name;
}
@Override
public void rule() {
System.out.println(name + "要遵章守法,不能随意外出,酗酒,约会~~~");
}
@Override
public void run() {
System.out.println(name + "必须要跑步训练~~");
}
@Override
public void competition() {
System.out.println(name + "需要参加国际比赛~~");
}
}
package com.itheima.d11_interface_implements;
public class Test {
public static void main(String[] args) {
PingPongMan p = new PingPongMan("张继科");
p.rule();
p.run();
p.competition();
}
}
接口与接口的关系:多继承
package com.itheima.d12_interface_extends;
public interface Law {
void rule(); // 遵章守法
void eat();
}
package com.itheima.d12_interface_extends;
public interface People {
void eat();
}
package com.itheima.d12_interface_extends;
public interface SportMan extends Law, People {
void run();
void competition();
}
package com.itheima.d12_interface_extends;
// public class BasketballMan implements Law, SportMan, People {
public class BasketballMan implements SportMan{
@Override
public void rule() {
}
@Override
public void eat() {
}
@Override
public void run() {
}
@Override
public void competition() {
}
}
JDK8开始接口新增方法
package com.itheima.d13_interface_jdk8;
public interface SportManInter {
default void run(){
//go();
System.out.println("跑的很快~~~");
}
static void inAddr(){
System.out.println("我们都在学习Java新增方法的语法,它是Java源码自己会用到的~~~");
}
//void go(){
// System.out.println("开始跑~~~");
//}
}
class PingPongMan implements SportManInter{
}
class Test{
public static void main(String[] args) {
PingPongMan p = new PingPongMan();
p.run();
SportManInter.inAddr();
// PingPongMan.inAddr();
}
}
使用接口的注意事项【了解】
package com.itheima.d14_interface_attention;
public class Test {
public static void main(String[] args) {
// 1、接口不能创建对象(接口更加彻底的抽象)
// 2、一个类实现多个接口,多个接口中有同样的静态方法不冲突。
// 3、一个类继承了父类,同时又实现了接口,父类中和接口中有同名方法,默认用父类的。
Cat c = new Cat();
c.eat();
// 4、一个类实现了多个接口,多个接口中存在同名的默认方法,不冲突,这个类重写该方法即可。
// 5、一个接口继承多个接口,是没有问题的,如果多个接口中存在规范冲突则不能多继承。
}
}
// 5、一个接口继承多个接口,是没有问题的,如果多个接口中存在规范冲突则不能多继承。
//interface AAA{
// int run();
//}
//interface BBB{
// void run();
//}
//
//interface CCC extends AAA, BBB{
//}
// 4、一个类实现了多个接口,多个接口中存在同名的默认方法,不冲突,这个类重写该方法即可。
interface AA{
default void go(){
System.out.println("AA");
}
}
interface BB{
default void go(){
System.out.println("BB");
}
}
class CC implements AA, BB{
@Override
public void go() {
}
}
// 3、一个类继承了父类,同时又实现了接口,父类中和接口中有同名方法,默认用父类的。
interface Food{
default void eat(){
System.out.println("接口中的吃方法~~");
}
}
class Animal{
public void eat(){
System.out.println("父类动物吃~~");
}
}
class Cat extends Animal implements Food {
}
interface A{
static void test(){
System.out.println("A");
}
}
interface B{
static void test(){
System.out.println("B");
}
}
class C implements A,B{
}



