赞
踩
1.关于final:当final修饰的是引用变量的时候, 是引用变量的值不能在变
2. 对象名(引用变量) instanceof 类
对于任何类型的引用变量 = null
null instanceof 目标类型 结果确定永远是false
public class Demo { public static void main(String[] args) { //关于final keyFinal(); // 测试 null instanceof 目标类型 Demo d = null; System.out.println(d instanceof Demo); //false System.out.println(null instanceof Demo); //false } private static void keyFinal() { final A a = new A(); // final修饰的引用变量本身不能在改变 //a = new A(); //final的修饰的引用变量所指向的对象的成员变量值,仍然可以改变 a.i = 100; a.i = 1000; } } class A { int i; }
···························································································································································································································
抽象类:回想之前的猫狗案例,动物本身并不是一个具体的事物,而是一个抽象的事物。只有真正的猫,狗才是具体的动物。所以,我们可以把动物类设置为抽象类。
abstract class Animal {
String name = "xxxAnimal";
/*
对于Animal而言,我无法笼统的回答动物吃什么,因为每种具体的动物,吃的东西都不一样
,但是我们确知道,动物应该有eat的行为,因此,在Animal类中,我们只应该声明动物有这样的行为
(只保留方法声明,而没有方法体)
只有方法声明的方法,他不能单独存在,必须加abstract关键字修饰,让该方法变成抽象方法
*/
public abstract void eat();
public abstract void shout();
}
···························································································································································································································
抽象类:包含有抽象方法的类 或 被abstract修饰的类
抽象类和抽象方法必须用abstract关键字修饰
abstract class 类名 {}
public abstract void eat();
特征:
抽象类不能直接实例化: new 抽象类
但是抽象类可以间接实例化:
抽象类类型 引用 = new 具体子类();
抽象类的子类:
a. 可以是具体类 (子类必须覆盖实现抽象父类中,所有的抽象方法)
b. 抽象类的子类可以是抽象类 (当子类没有覆盖并实现,抽象父类中所有的抽象方法)
注意事项:
a. 有抽象方法的类一定是抽象类
b. 抽象类不一定有抽象方法
抽象类不能被实例化,不能创建抽象类的类对象
eat()和shut()方法运行后一定不是抽象类中的eat()和shut()方法,一定是其子类的eat()和shut()方法,因为抽象类中的eat()和shut()并没有实现,而子类通过方法覆盖实现了这些方法。所以说,抽象类和多态天然有关系。
··························································································································································································································
抽象类VS普通类
抽象类 VS 普通类的成员特点:
构造方法:抽象类同普通类
成员变量:抽象类同普通类
成员方法:抽象类:
可以是抽象方法: 该类中有这样的行为,但是该类还不确定这样的行为如何实现
也可以是非抽象方法:非抽象方法,主要实现的是代码复用
普通类:非抽象方法
抽象类不能实例化,为啥有构造方法?
··························································································································································································································
对于为什么冲突的理解:
abstract 定义抽象方法,对于抽象方法而言,如果在代码中要使用,其实永远是通过多态,调用的是子类中,覆盖实现的抽象父类的抽象方法
而被,private,final,static关键字修饰的方法,都不能在子类中被覆盖,于是意味着这些方法,无法在程序中运行。
··························································································································································································································
接口的语法
1.接口用关键字interface表示
格式:interface 接口名 {}
2.在Java语言中interface也可以表示一种数据类型
a. 类和接口都可以用来表示数据类型(类和接口是地位对等的),只不过他们的侧重点不一样
操作(行为)描述:
b. 类定义的是一个数据集合基于这个数据集的 一组操作(行为),
类所描述的这一组行为,它们是由关系的(间接),都可以访问同一个数据集合(成员变量)
c. 接口表示数据类型,侧重于描述,一组具有特殊 功能 的行为,这些行为,可以完全没有任何关系。接口中的方法,它们的关系比较的松散
3.类实现接口用implements表示
类 和 接口,可以有实现关系,实现关系用implements表示
类和接口的实现关系(类可以实现接口):实现关系其实是一种实质上的继承关系
格式:class 类名 implements 接口名 {}
4.接口不能直接实例化(接口中,对于方法,只能有抽象方法)
a. 不能直接实例化一个接口(new 接口)
b. 接口可以间接实例化
接口类型的 引用 = new 接口实现子类()
MyInterface inter = new InterfaceClass();
5.接口的子类,
a.可以是抽象类
b.也可以是具体类
··························································································································································································································
注:接口里面必须定义抽象方法,而抽象类里面可以定义普通方法
接口的组成特点:
无构造方法
成员变量:只能是常量,默认修饰符 public static final
成员方法:只能是抽象方法,默认修饰符 public abstract
··························································································································································································································
回顾一下,之前我们讲类的时候说过,Java中类与类之间只能单重继承。
那么是不是Java中,只有单重继承呢? 不是
之所以,java声称自己实现了多重继承
}
··························································································································································································································
抽象类和接口的比较
成员区别
抽象类 成员变量:没有任何限制;
成员方法:有抽象方法; 非抽象方法
接口 成员变量:只能是常量,默认修饰符 public static final;
成员方法:必须是抽象方法(jdk7及以前成立)
关系区别
类与类(包括普通类和抽象类) 继承关系,单继承
类与接口 实现关系, 单实现(实现一个接口),多实现(实现多个接口)
接口与接口 继承关系,单继承,多继承
设计理念区别
抽象类 被其他类继承体现的是:“is a”的关系。
表示共性功能
接口 被实现体现的是:”like a”的关系。
扩展功能
说明is a 和like 差别:
is a:
一个子类 只能继承 一个父类,继承在最理想的使用情况下,是子类不自己定义自己成员,此时在理想情况下,其实父类和子类,是完全相互替代的
父类引用/子类引用 = new 子类对象();
like a:
因为,一个类可以同时实现多个接口,每个接口只表示子类中,的一部分成员, 通过一个接口只能看到一个类的冰山一角
··························································································································································································································
接口和类一样,只能被默认访问权限和public访问权限修饰
(其实这两种方法并不常用)
从jdk8开始,接口中,可以定义两种特殊的方法(特殊就特殊在,这两种方法可以有方法体)
···························································································································································································································
作业:
1.为某研究所编写一个通用程序,用来计算每一种交通工具运行 1000公里所需的时间.已知每种交通工具的参数都是3个整数常量A、B、C的表达式。
现有两种工具:Car 和Plane,
其中Car 的速度运算公式为:A*B/C,
Plane 的速度运算公式为 :A+B+C。
要求在未来如果增加第3种交通工具(如Ship)的时候,可以做到复用已有代码。
Ship的速度运算公式为 :A+B/C。
要求自己设计该系统,并测试。
package com.cskaoyan.Interface; /** * @author shihao * @create 2020-04-21 9:29 */ public class Homework01 { public static void main(String[] args) { Vehicle car = new Car(10, 5, 3); Vehicle plane = new Plane(10, 5, 3); Vehicle ship = new Ship(10, 5, 3); System.out.println("car speed:" + car.speed() + " time:" + 1000 / car.speed()); System.out.println("Plane speed:" + plane.speed() + " time:" + 1000 / plane.speed()); System.out.println("Ship speed:" + ship.speed() + " time:" + 1000 / ship.speed()); } } abstract class Vehicle { protected int A; protected int B; protected int C; public Vehicle(int a, int b, int c) { A = a; B = b; C = c; } public int getA() { return A; } public int getB() { return B; } public int getC() { return C; } public void setA(int a) { A = a; } public void setB(int b) { B = b; } public void setC(int c) { C = c; } abstract public int speed(); } class Car extends Vehicle { public Car(int a, int b, int c) { super(a, b, c); } @Override public int speed() { return super.A * super.B / super.C; } } class Plane extends Vehicle { public Plane(int a, int b, int c) { super(a, b, c); } @Override public int speed() { return super.getA() + super.getB() + super.getC(); } } class Ship extends Vehicle { public Ship(int a, int b, int c) { super(a, b, c); } @Override public int speed() { return super.getA() + super.getB() / super.getC(); } }
package com.cskaoyan.Interface; /** * @author shihao * @create 2020-04-20 22:38 * <p> * 教练和运动员案例 * <p> * 乒乓球运动员和篮球运动员。 * 乒乓球教练和篮球教练。 * <p> * 为了出国交流,跟乒乓球相关的人员都需要学习英语(一个扩展的行为,特殊的功能)。 * interface specialSkill { * void learningEnglish() * } * <p> * <p> * 请用所学知识: * 分析,这个案例中有哪些抽象类,哪些接口,哪些具体类。 * <p> * <p> * 乒乓球运动员 * 属性:name, age, salary * 行为:eat(), sleep(), train(), match() * <p> * 乒乓球教练 * 属性:name, age, salary, bonus * 行为:eat(), sleep(), teach() * <p> * 足球运动员 * 属性:name, age, salary * 行为:eat(), sleep(), train(), match() * <p> * 足球教练 * 属性:name, age, salary, bonus * 行为:eat(), sleep(), teach() * <p> * 从共性: * 1. 所有的运动员和教练员 定义一个所有运动员和教练员都要继承公共父类 Human普通 * 有相同的属性:name, age, salary * 有相同的行为:eat(), sleep() * <p> * <p> * 2. 定义类来描述运动员和教练员 * a. 教练员和运动员 继承 Human * <p> * b. 对于教练员而言: 定义一个类 Coach 抽象类 extend Human * 多了一个属性 bonus * 多了一个行为:teach(),同时注意到不同类型的教练员,teach的内容不一样,抽象方法 * <p> * c. 对于运动员而言: 定义一个类 Athlete 抽象类 extends Human * 多了运动员特有的行为:train(), match() * 但是,不同类型的有动员,但是训练和比赛的内容是不一样,train(), match()抽象方法 * <p> * <p> * 3. 分别定义 * 乒乓球运动员:TableTennisAthlete extends Athlete implements specialSkill, 继承之后,事项 train(), match() * 乒乓球教练: TableTennisCoach extends Coach implements specialSkill, 继承只需要实现 teach() * 足球运动员: FootballAthlete extends Athlete, 继承之后,事项 train(), match() * 足球教练 FootballCoach extends Coach, 继承只需要实现 teach() */ public class Demo06Exercise { public static void main(String[] args) { Human tabletennisAthlete = new TableTennisAthlete("jike zhang", 28, 4000); Human tabletennisCoath = new TableTennisCoach("guoliang liu", 74, 8000, 200); Human footballAthlete = new FootballAthlete("Messi", 32, 50000); Human footballCoach = new FootballCoach("wu", 51, 2000, 200); ((TableTennisAthlete) tabletennisAthlete).learningEnglish(); ((TableTennisCoach) tabletennisCoath).learningEnglish(); ((FootballAthlete) footballAthlete).match(); ((FootballCoach) footballCoach).teach(); } } class Human { protected String name; protected int age; protected int salary; public Human(String name, int age, int salary) { this.name = name; this.age = age; this.salary = salary; } public void eat() { System.out.println(this.getName() + " is eating"); } public void sleep() { System.out.println(this.getName() + " is sleeping"); } public String getName() { return name; } public int getAge() { return age; } public int getSalary() { return salary; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSalary(int salary) { this.salary = salary; } } abstract class Athlete extends Human { public Athlete(String name, int age, int salary) { super(name, age, salary); } abstract public void match(); abstract public void train(); } abstract class Coach extends Human { protected int bonus; public Coach(String name, int age, int salary, int bonus) { super(name, age, salary); this.bonus = bonus; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } abstract public void teach(); } interface specialSkill { void learningEnglish(); } class TableTennisAthlete extends Athlete implements specialSkill { public TableTennisAthlete(String name, int age, int salary) { super(name, age, salary); } @Override public void match() { System.out.println("TableTennisAthlete " + this.getName() + " is matching"); } @Override public void train() { System.out.println("TableTennisAthlete " + this.getName() + " is training"); } @Override public void learningEnglish() { System.out.println("TableTennisAthlete " + this.getName() + " is learning English"); } } class TableTennisCoach extends Coach implements specialSkill { public TableTennisCoach(String name, int age, int salary, int bonus) { super(name, age, salary, bonus); } @Override public void teach() { System.out.println("TableTennisCoach " + this.getName() + " is teaching"); } @Override public void learningEnglish() { System.out.println("TableTennisCoach " + this.getName() + " is learning English"); } } class FootballAthlete extends Athlete { public FootballAthlete(String name, int age, int salary) { super(name, age, salary); } @Override public void match() { System.out.println("FootballAthlete " + this.getName() + " is matching"); } @Override public void train() { System.out.println("FootballAthlete " + this.getName() + " is learning English"); } } class FootballCoach extends Coach { public FootballCoach(String name, int age, int salary, int bonus) { super(name, age, salary, bonus); } @Override public void teach() { System.out.println("FootballCoach " + this.getName() + " is teaching"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。