Java基础第九天
继承概述引入 首先我来写两个代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //定义学生类
class Student
{
public void study(){
System.out.println( "在教室学习" );
}
}
//定义老师类
class Teacher
{
public void teach(){
System.out.println( "在教室教书" );
}
}
|
我们观察上面两个代码: 发现name,age成员变量,以及getXxx()/setXxx(),还有eat()等都是相同的。 如果我们后来继续定义类,举例:工人类,军人类。他们是不是也具备这些内容。 那么,我们每一次定义这样的类的时候,都要把这些重复的内容都重新定义一遍。 麻烦不?麻烦。所以,我们要考虑改进? 如何改进呢? 我这样想的:我能不能把这些相同的内容给定义到一个独立的类中。
然后,让这多个类和这个独立的类产生一个关系,有了这个关系后, 这多个类就可以具备这个独立的类的功能。 为了实现这个效果,java就提供了一个技术:继承。
父亲: 4个儿子 继承怎么表示呢?继承的格式是什么样子的呢? 1 2 3 4 5 | class Fu {}
class Zi extends Fu {
}
|
我们就回头修改我们的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class Person
{
String name;
int age;
public void eat(){
System.out.println( "吃饭" );
}
}
class Student extends Person
{
public void study(){
System.out.println( "在教室学习" );
}
}
class Teacher extends Person
{
public void teach(){
System.out.println( "在教室教书" );
}
}
class Worker extends Person
{
public void work(){
System.out.println( "在工作" );
}
}
|
继承概述多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可。 通过extends关键字可以实现类与类的继承 class 子类名 extends 父类名 {} 单独的这个类称为父类,基类或者超类;这多个类可以称为子类或者派生类。 有了继承以后,我们定义一个类的时候,可以在一个已经存在的类的基础上,还可以定义自己的新成员。
继承的案例和继承的好处通过一个具体案例来演示代码 案例1:学生类和老师。定义两个功能(吃饭,睡觉) 案例2:加入人类后改进。
/* 继承概述: 把多个类中相同的内容给提取出来定义到一个类中。
如何实现继承呢? Java提供了关键字:extends
格式: class 子类名 extends 父类名 {}
好处: A:提高了代码的复用性 B:提高了代码的维护性 C:让类与类之间产生了关系,是多态的前提 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | class Person
{
String name;
int age;
public void eat(){
System.out.println( "吃饭" );
}
}
class Student extends Person
{
public void study(){
System.out.println( "在教室学习" );
}
}
class Teacher extends Person
{
public void teach(){
System.out.println( "在教室教书" );
}
}
class Worker extends Person
{
public void work(){
System.out.println( "在工作" );
}
}
class PersonTest
{
public static void main(String[] args)
{
Student s = new Student();
s.eat();
s.study();
Teacher t = new Teacher();
t.eat();
t.teach();
Worker w = new Worker();
w.eat();
w.work();
System.out.println( "Hello World!" );
}
}
|
继承的好处提高了代码的复用性 多个类相同的成员可以放到同一个类中 提高了代码的维护性 如果功能的代码需要修改,修改一处即可 让类与类之间产生了关系,是多态的前提 Java中继承的特点Java只支持单继承,不支持多继承。 一个类只能有一个父类,不可以有多个父类。 class SubDemo extends Demo{} //ok class SubDemo extends Demo1,Demo2...//error Java支持多层继承(继承体系) class A{} class B extends A{} class C extends B{} /* Java中继承的特点: A:Java只支持单继承,不支持多继承。 有些语言是支持多继承,格式:extends 类1,类2,... B:Java支持多层继承(继承体系) */ /* class Father {} class Mother {} class Son exnteds Father {} //正确的 class Son extends Father,Mother {} // 错误的 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class GrandFather {
public void show() {
System.out.println( "我是爷爷" );
}
}
class Father extends GrandFather {
public void method(){
System.out.println( "我是老子" );
}
}
class Son extends Father {}
class ExtendsDemo2 {
public static void main(String[] args) {
Son s = new Son();
s.method(); //使用父亲的
s.show(); //使用爷爷的
}
}
|
Java中继承的注意事项子类只能继承父类所有非私有的成员(成员方法和成员变量) 其实这也体现了继承的另一个弊端:打破了封装性 子类不能继承父类的构造方法,但是可以通过super(后面讲)关键字去访问父类构造方法。 不要为了部分功能而去继承 我们到底在什么时候使用继承呢? 继承中类之间体现的是:”is a”的关系。 /* 继承的注意事项: A:子类只能继承父类所有非私有的成员(成员方法和成员变量) B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法。 C:不要为了部分功能而去继承 class A { public void show1(){} public void show2(){} }
class B { public void show2(){} public void show3(){} }
//我们发现B类中出现了和A类一样的show2()方法,所以,我们就用继承来体现 class B extends A { public void show3(){} } 这样其实不好,因为这样你不但有了show2(),还多了show1()。 有可能show1()不是你想要的。
那么,我们什么时候考虑使用继承呢? 继承其实体现的是一种关系:"is a"。 Person Student Teacher 水果 苹果 香蕉 橘子
采用假设法。 如果有两个类A,B。只有他们符合A是B的一种,或者B是A的一种,就可以考虑使用继承。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | class GrandFather
{
public int num1 = 10 ;
public void show(){
System.out.println( "我是爷爷" );
}
}
class Father extends GrandFather
{
public int num2 = 100 ;
public int num1 = 100000 ;
public void method(){
int num3 = 1000 ;
int num1 = 20 ;
System.out.println( "我是老子" );
System.out.println( super .num1);
System.out.println(num3);
System.out.println(num1);
System.out.println( this .num1);
}
}
class Son extends Father
{
public void fuction(){
System.out.println( super .num1);
System.out.println(num2);
}
}
class ExtendsDemo2
{
public static void main(String[] args)
{
Son son = new Son();
son.show();
son.method();
son.fuction();
System.out.println( "Hello World!" );
}
}
|
继承中成员变量的关系 案例演示 子父类中同名和不同名的成员变量 /* 类的组成: 成员变量: 构造方法: 成员方法: 而现在我们又讲解了继承,所以,我们就应该来考虑一下,类的组成部分的各自关系。
继承中成员变量的关系: A:子类中的成员变量和父类中的成员变量名称不一样,这个太简单。 B:子类中的成员变量和父类中的成员变量名称一样,这个怎么玩呢? 在子类方法中访问一个变量的查找顺序: a:在子类方法的局部范围找,有就使用 b:在子类的成员范围找,有就使用 c:在父类的成员范围找,有就使用 d:如果还找不到,就报错。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Father {
public int num = 10 ;
}
class Son extends Father {
public int num = 20 ;
public void show() {
int num = 30 ;
System.out.println(num);
System.out.println( this .num);
System.out.println( super .num);
}
}
class ExtendsDemo5 {
public static void main(String[] args) {
Son s = new Son();
s.show();
}
}
|
继承中构造方法的关系子类中所有的构造方法默认都会访问父类中空参数的构造方法 为什么呢? 因为子类会继承父类中的数据,可能还会使用父类的数据。所以,子类初始化之前,一定要先完成父类数据的初始化。 每一个构造方法的第一条语句默认都是:super() /* 继承中构造方法的关系 A:子类中所有的构造方法默认都会访问父类中空参数的构造方法 B:为什么呢? 因为子类会继承父类中的数据,可能还会使用父类的数据。 所以,子类初始化之前,一定要先完成父类数据的初始化。
注意:子类每一个构造方法的第一条语句默认都是:super(); */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Father {
int age;
public Father() {
System.out.println( "Father的无参构造方法" );
}
public Father(String name) {
System.out.println( "Father的带参构造方法" );
}
}
class Son extends Father {
public Son() {
//super();
System.out.println( "Son的无参构造方法" );
}
public Son(String name) {
//super();
System.out.println( "Son的带参构造方法" );
}
}
class ExtendsDemo6 {
public static void main(String[] args) {
//创建对象
Son s = new Son();
System.out.println( "------------" );
Son s2 = new Son( "林青霞" );
}
}
|
如何父类中没有构造方法,该怎么办呢? 子类通过super去显示调用父类其他的带参的构造方法 子类通过this去调用本类的其他构造方法 本类其他构造也必须首先访问了父类构造 一定要注意: super(…)或者this(….)必须出现在第一条语句上 否则,就会有父类数据的多次初始化 /* 如果父类没有无参构造方法,那么子类的构造方法会出现什么现象呢? 报错。 如何解决呢? A:在父类中加一个无参构造方法 B:通过使用super关键字去显示的调用父类的带参构造方法 C:子类通过this去调用本类的其他构造方法 子类中一定要有一个去访问了父类的构造方法,否则父类数据就没有初始化。
注意事项: this(...)或者super(...)必须出现在第一条语句上。 如果不是放在第一条语句上,就可能对父类的数据进行了多次初始化,所以必须放在第一条语句上。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Father {
/*
public Father() {
System.out.println("Father的无参构造方法");
}
*/
public Father(String name) {
System.out.println( "Father的带参构造方法" );
}
}
class Son extends Father {
public Son() {
super ( "随便给" );
System.out.println( "Son的无参构造方法" );
//super("随便给");
}
public Son(String name) {
//super("随便给");
this ();
System.out.println( "Son的带参构造方法" );
}
}
class ExtendsDemo7 {
public static void main(String[] args) {
Son s = new Son();
System.out.println( "----------------" );
Son ss = new Son( "林青霞" );
}
}
|
面试题一 /* 看程序写结果: A:成员变量就近原则 B:this和super的问题 this访问本类的成员 super访问父类的成员 C:子类构造方法执行前默认先执行父类的无参构造方法 D:一个类的初始化过程 成员变量进行初始化 默认初始化 显示初始化 构造方法初始化
结果: fu zi 30 20 10 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class Fu
{
public int num = 10 ;
public Fu(){
System.out.println( "fu" );
}
}
class Zi extends Fu
{
public int num = 20 ;
public Zi(){
System.out.println( "Zi" );
}
public void show(){
int num = 30 ;
System.out.println(num); //30
System.out.println( this .num); //20
System.out.println( super .num); //10
}
}
class ExtendsTest
{
public static void main(String[] args)
{
Zi z = new Zi();
z.show();
System.out.println( "Hello World!" );
}
}
|
面试题二/* 看程序写结果: A:一个类的静态代码块,构造代码块,构造方法的执行流程 静态代码块 > 构造代码块 > 构造方法 B:静态的内容是随着类的加载而加载 静态代码块的内容会优先执行 C:子类初始化之前先会进行父类的初始化
结果是: 静态代码块Fu 静态代码块Zi 构造代码块Fu 构造方法Fu 构造代码块Zi 构造方法Zi */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Fu
{
static {
System.out.println( "静态代码块Fu" );
}
{
System.out.println( "构造代码块Fu" );
}
public Fu(){
System.out.println( "构造方法Fu" );
}
}
class Zi extends Fu
{
static {
System.out.println( "静态代码块Zi" );
}
{
System.out.println( "构造代码块Zi" );
}
public Zi(){
//super();
System.out.println( "构造方法Zi" );
}
}
class ExtendsTest2
{
public static void main(String[] args)
{
Zi z = new Zi();
System.out.println( "Hello World!" );
}
}
|
继承中成员方法的关系案例演示 子父类中同名和不同名的成员方法 结论: 通过子类对象去访问一个方法 首先在子类中找 然后在父类中找 如果还是没有就报错。(不考虑父亲的父亲…) /* 继承中成员方法的关系: A:子类中的方法和父类中的方法声明不一样,这个太简单。 B:子类中的方法和父类中的方法声明一样,这个该怎么玩呢? 通过子类对象调用方法: a:先找子类中,看有没有这个方法,有就使用 b:再看父类中,有没有这个方法,有就使用 c:如果没有就报错。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Father {
public void show() {
System.out.println( "show Father" );
}
}
class Son extends Father {
public void method() {
System.out.println( "method Son" );
}
public void show() {
System.out.println( "show Son" );
}
}
class ExtendsDemo8 {
public static void main(String[] args) {
//创建对象
Son s = new Son();
s.show();
s.method();
//s.fucntion(); //找不到符号
}
}
|
方法重写概述方法重写概述 子类中出现了和父类中一模一样的方法声明,也被称为方法覆盖,方法复写。 使用特点: 如果方法名不同,就调用对应的方法 如果方法名相同,最终使用的是子类自己的 方法重写的应用: 当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法,这样,即沿袭了父类的功能,又定义了子类特有的内容。 /* 方法重写:子类中出现了和父类中方法声明一模一样的方法。
方法重载: 本类中出现的方法名一样,参数列表不同的方法。与返回值无关。 子类对象调用方法的时候: 先找子类本身,再找父类。
方法重写的应用: 当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法。 这样,即沿袭了父类的功能,又定义了子类特有的内容。
案例: A:定义一个手机类。 B:通过研究,我发明了一个新手机,这个手机的作用是在打完电话后,可以听天气预报。 按照我们基本的设计,我们把代码给写出来了。 但是呢?我们又发现新手机应该是手机,所以,它应该继承自手机。 其实这个时候的设计,并不是最好的。 因为手机打电话功能,是手机本身就具备的最基本的功能。 所以,我的新手机是不用在提供这个功能的。 但是,这个时候,打电话功能就没有了。这个不好。 最终,还是加上这个功能。由于它继承了手机类,所以,我们就直接使用父类的功能即可。 那么,如何使用父类的功能呢?通过super关键字调用 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Phone {
public void call(String name) {
System.out.println( "给" +name+ "打电话" );
}
}
class NewPhone extends Phone {
public void call(String name) {
//System.out.println("给"+name+"打电话");
super .call(name);
System.out.println( "可以听天气预报了" );
}
}
class ExtendsDemo9 {
public static void main(String[] args) {
NewPhone np = new NewPhone();
np.call( "林青霞" );
}
}
|
方法重写的注意事项父类中私有方法不能被重写 子类重写父类方法时,访问权限不能更低 父类静态方法,子类也必须通过静态方法进行重写。(其实这个算不上方法重写,但是现象确实如此,至于为什么算不上方法重写,多态中我会讲解) /* 方法重写的注意事项 A:父类中私有方法不能被重写 因为父类私有方法子类根本就无法继承 B:子类重写父类方法时,访问权限不能更低 最好就一致 C:父类静态方法,子类也必须通过静态方法进行重写 其实这个算不上方法重写,但是现象确实如此,至于为什么算不上方法重写,多态中我会讲解
子类重写父类方法的时候,最好声明一模一样。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | class Father {
//private void show() {}
/*
public void show() {
System.out.println("show Father");
}
*/
void show() {
System.out.println("show Father");
}
/*
public static void method() {
}
*/
public void method() {
}
}
class Son extends Father {
//private void show() {}
/*
public void show() {
System.out.println("show Son");
}
*/
public void show() {
System.out.println("show Son");
}
public static void method() {
}
/*
public void method() {
}
*/
}
class ExtendsDemo10 {
public static void main(String[] args) {
Son s = new Son();
s.show();
}
}
|
面试题1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象。
方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象。 方法重载能改变返回值类型,因为它和返回值类型无关。 Override:方法重写 Overload:方法重载 2:this关键字和super关键字分别代表什么?以及他们各自的使用场景和作用。 this:代表当前类的对象引用 super:代表父类存储空间的标识。(可以理解为父类的引用,通过这个东西可以访问父类的成员) 场景: 成员变量: this.成员变量 super.成员变量 构造方法: this(...) super(...) 成员方法: this.成员方法 super.成员方法 继承练习学生案例和老师案例讲解 使用继承前 使用继承后 父类中成员private修饰,子类如何访问呢? /* 学生案例和老师案例讲解
学生: 成员变量;姓名,年龄 构造方法:无参,带参 成员方法:getXxx()/setXxx() 老师: 成员变量;姓名,年龄 构造方法:无参,带参 成员方法:getXxx()/setXxx()
看上面两个类的成员,发现了很多相同的东西,所以我们就考虑抽取一个共性的类: 人: 成员变量;姓名,年龄 构造方法:无参,带参 成员方法:getXxx()/setXxx()
学生 继承 人 老师 继承 人 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | class Person
{
//姓名
private String name;
private int age;
public Person(){
}
public Person(String name, int age){
this .name = name;
this .age = age;
}
public void setName(String name){
this .name = name;
}
public String getName(){
return name;
}
public void setAge( int age){
this .age = age;
}
public int getAge(){
return age;
}
}
class Student extends Person
{
public Student(){
}
public Student(String name, int age){
super (name,age);
}
}
class Teacher extends Person
{
public Teacher(){
}
public Teacher(String name, int age){
super (name,age);
}
}
class Worker extends Person
{
public Worker(){
}
public Worker(String name, int age){
super (name,age);
}
}
class ExtendsTest4
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.setName( "林青霞" );
s1.setAge( 27 );
System.out.println(s1.getName()+ " \t" +s1.getAge());
Student s2 = new Student( "杨幂" , 23 );
System.out.println(s2.getName()+ " \t" +s2.getAge());
Teacher t1 = new Teacher();
t1.setName( "邓丽君" );
t1.setAge( 38 );
System.out.println(t1.getName()+ " \t" +t1.getAge());
Teacher t2 = new Teacher( "张三丰" , 54 );
System.out.println(t2.getName()+ " \t" +t2.getAge());
Worker w1 = new Worker();
w1.setName( "杨过" );
w1.setAge( 25 );
System.out.println(w1.getName()+ " \t" +w1.getAge());
Worker w2 = new Worker( "李思思" , 36 );
System.out.println(w2.getName()+ " \t" +w2.getAge());
}
}
|
猫狗案例讲解分析和实现 /* 猫狗案例讲解
先找到具体的事物,然后发现具体的事物有共性,才提取出一个父类。
猫: 成员变量:姓名,年龄,颜色 构造方法:无参,带参 成员方法: getXxx()/setXxx() eat() palyGame() 狗: 成员变量:姓名,年龄,颜色 构造方法:无参,带参 成员方法: getXxx()/setXxx() eat() lookDoor()
共性: 成员变量:姓名,年龄,颜色 构造方法:无参,带参 成员方法: getXxx()/setXxx() eat()
把共性定义到一个类中,这个类的名字叫:动物。 动物类: 成员变量:姓名,年龄,颜色 构造方法:无参,带参 成员方法: getXxx()/setXxx() eat()
猫: 构造方法:无参,带参 成员方法:palyGame() 狗: 构造方法:无参,带参 成员方法:lookDoor() */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | class Animal
{
private String name;
private int age;
private String color;
public Animal(){}
public Animal(String name, int age,String color){
this .name = name;
}
public void setName(String name){
this .name = name;
this .age = age;
this .color = color;
}
public String getName(){
return name;
}
public void setAge( int age){
this .age = age;
}
public int getAge(){
return age;
}
public void setColor(String color){
this .color = color;
}
public String getColor(){
return color;
}
public void eat(){
System.out.println( "别睡了,起来吃饭吧" );
}
}
class Cat extends Animal
{
public Cat(){}
public Cat(String name, int age,String color){
super (name,age,color);
}
public void playGame(){
System.out.println( "猫玩英雄联盟" );
}
}
class Dog extends Animal
{
public Dog(){}
public Dog(String name, int age,String color){
super (name,age,color);
}
public void lookDoor(){
System.out.println( "狗看家" );
}
}
class ExtendsTest5
{
public static void main(String[] args)
{
Cat c1 = new Cat();
c1.setName( "Tom" );
c1.setAge( 3 );
c1.setColor( "白色" );
System.out.println( "猫的名字是:" +c1.getName()+ ";年龄是:" +c1.getAge()+ ";颜色是:" +c1.getColor());
c1.eat();
c1.playGame();
System.out.println( "---------------" );
Cat c2 = new Cat( "杰瑞" , 5 , "土豪金" );
System.out.println( "猫的名字是:" +c2.getName()+ ";年龄是:" +c2.getAge()+ ";颜色是:" +c2.getColor());
c2.eat();
c2.playGame();
System.out.println( "---------------" );
Dog d1 = new Dog();
d1.setName( "Tom" );
d1.setAge( 3 );
d1.setColor( "白色" );
System.out.println( "狗的名字是:" +d1.getName()+ ";年龄是:" +d1.getAge()+ ";颜色是:" +d1.getColor());
d1.eat();
d1.lookDoor();
System.out.println( "---------------" );
Dog d2 = new Dog( "杰瑞" , 5 , "土豪金" );
System.out.println( "狗的名字是:" +d2.getName()+ ";年龄是:" +d2.getAge()+ ";颜色是:" +d2.getColor());
d2.eat();
d2.lookDoor();
}
}
|
final关键字 final关键字是最终的意思,可以修饰类,成员变量,成员方法。 修饰类,类不能被继承 修饰变量,变量就变成了常量,只能被赋值一次 修饰方法,方法不能被重写 /* 继承的代码体现
由于继承中方法有一个现象:方法重写。 所以,父类的功能,就会被子类给覆盖调。 有些时候,我们不想让子类去覆盖掉父类的功能,只能让他使用。 这个时候,针对这种情况,Java就提供了一个关键字:final
final:最终的意思。常见的是它可以修饰类,方法,变量。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Fu {
public final void show() {
System.out.println( "这里是绝密资源,任何人都不能修改" );
}
}
class Zi extends Fu {
// Zi中的show()无法覆盖Fu中的show()
public void show() {
System.out.println( "这是一堆垃圾" );
}
}
class ZiDemo {
public static void main(String[] args) {
Zi z = new Zi();
z.show();
}
}
|
Final特性/* final可以修饰类,方法,变量
特点: final可以修饰类,该类不能被继承。 final可以修饰方法,该方法不能被重写。(覆盖,复写) final可以修饰变量,该变量不能被重新赋值。因为这个变量其实常量。
常量: A:字面值常量 "hello",10,true B:自定义常量 final int x = 10; */ //final class Fu //无法从最终Fu进行继承 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class Fu {
public int num = 10 ;
public final int num2 = 20 ;
/*
public final void show() {
}
*/
}
class Zi extends Fu {
// Zi中的show()无法覆盖Fu中的show()
public void show() {
num = 100 ;
System.out.println(num);
//无法为最终变量num2分配值
//num2 = 200;
System.out.println(num2);
}
}
class FinalDemo {
public static void main(String[] args) {
Zi z = new Zi();
z.show();
}
}
|
final关键字面试题final修饰局部变量 在方法内部,该变量不可以被改变 在方法声明上,分别演示基本类型和引用类型作为参数的情况 基本类型,是值不能被改变 引用类型,是地址值不能被改变 /* 面试题:final修饰局部变量的问题 基本类型:基本类型的值不能发生改变。 引用类型:引用类型的地址值不能发生改变,但是,该对象的堆内存的值是可以改变的。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | class Student {
int age = 10 ;
}
class FinalTest {
public static void main(String[] args) {
//局部变量是基本数据类型
int x = 10 ;
x = 100 ;
System.out.println(x);
final int y = 10 ;
//无法为最终变量y分配值
//y = 100;
System.out.println(y);
System.out.println( "--------------" );
//局部变量是引用数据类型
Student s = new Student();
System.out.println(s.age);
s.age = 100 ;
System.out.println(s.age);
System.out.println( "--------------" );
final Student ss = new Student();
System.out.println(ss.age);
ss.age = 100 ;
System.out.println(ss.age);
//重新分配内存空间
//无法为最终变量ss分配值
ss = new Student();
}
}
|
final修饰变量的初始化时机 在对象构造完毕前即可 /* final修饰变量的初始化时机 A:被final修饰的变量只能赋值一次。 B:在构造方法完毕前。(非静态的常量) */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Demo {
//int num = 10;
//final int num2 = 20;
int num;
final int num2;
{
//num2 = 10;
}
public Demo() {
num = 100 ;
//无法为最终变量num2分配值
num2 = 200 ;
}
}
class FinalTest2 {
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.num);
System.out.println(d.num2);
}
}
|
多态多态概述某一个事物,在不同时刻表现出来的不同状态。 举例: 猫可以是猫的类型。猫 m = new 猫(); 同时猫也是动物的一种,也可以把猫称为动物。 动物 d = new 猫(); 在举一个例子:水在不同时刻的状态 多态前提和体现 有继承关系 有方法重写 有父类引用指向子类对象 多态案例及成员访问特点/* 多态:同一个对象(事物),在不同时刻体现出来的不同状态。 举例: 猫是猫,猫是动物。 水(液体,固体,气态)。
多态的前提: A:要有继承关系。 B:要有方法重写。 其实没有也是可以的,但是如果没有这个就没有意义。 动物 d = new 猫(); d.show(); 动物 d = new 狗(); d.show(); C:要有父类引用指向子类对象。 父 f = new 子();
用代码体现一下多态。
多态中的成员访问特点: A:成员变量 编译看左边,运行看左边。 B:构造方法 创建子类对象的时候,访问父类的构造方法,对父类的数据进行初始化。 C:成员方法 编译看左边,运行看右边。 D:静态方法 编译看左边,运行看左边。 (静态和类相关,算不上重写,所以,访问还是左边的)
由于成员方法存在方法重写,所以它运行看右边。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | class Fu {
public int num = 100 ;
public void show() {
System.out.println( "show Fu" );
}
public static void function() {
System.out.println( "function Fu" );
}
}
class Zi extends Fu {
public int num = 1000 ;
public int num2 = 200 ;
public void show() {
System.out.println( "show Zi" );
}
public void method() {
System.out.println( "method zi" );
}
public static void function() {
System.out.println( "function Zi" );
}
}
class DuoTaiDemo {
public static void main(String[] args) {
//要有父类引用指向子类对象。
//父 f = new 子();
Fu f = new Zi();
System.out.println(f.num);
//找不到符号
//System.out.println(f.num2);
f.show();
//找不到符号
//f.method();
f.function();
}
}
|
成员访问特点成员变量 编译看左边,运行看左边 成员方法 编译看左边,运行看右边 静态方法 编译看左边,运行看左边 所以前面我说静态方法不能算方法的重写 多态的好处 提高了程序的维护性(由继承保证) 提高了程序的扩展性(由多态保证) /* 多态的好处: A:提高了代码的维护性(继承保证) B:提高了代码的扩展性(由多态保证)
猫狗案例代码 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | class Animal
{
public void eat(){
System.out.println( "eat" );
}
public void sleep(){
System.out.println( "sleep" );
}
}
class Cat extends Animal
{
public void eat(){
System.out.println( "猫吃鱼" );
}
public void sleep(){
System.out.println( "猫趴着睡" );
}
}
class Dog extends Animal
{
public void eat(){
System.out.println( "狗吃肉" );
}
public void sleep(){
System.out.println( "狗站着睡" );
}
}
class Ultraman extends Animal
{
public void eat(){
System.out.println( "奥特曼打小怪兽" );
}
public void sleep(){
System.out.println( "奥特曼不用睡觉" );
}
}
class AnimalTools
{
public static void animalTools(Animal a){
a.eat();
a.sleep();
}
}
class DuoTai
{
public static void main(String[] args)
{
Animal cat = new Cat();
AnimalTools.animalTools(cat);
Animal dog = new Dog();
AnimalTools.animalTools(dog);
Animal ultraman = new Ultraman();
AnimalTools.animalTools(ultraman);
//System.out.println("Hello World!");
}
}
|
多态的弊端不能访问子类特有功能 那么我们如何才能访问子类的特有功能呢? 多态中的转型 /* 多态的弊端: 不能使用子类的特有功能。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Fu {
public void show() {
System.out.println( "show fu" );
}
}
class Zi extends Fu {
public void show() {
System.out.println( "show zi" );
}
public void method() {
System.out.println( "method zi" );
}
}
class DuoTaiDemo3 {
public static void main(String[] args) {
//测试
Fu f = new Zi();
f.show();
f.method();
}
}
|
多态中的转型问题向上转型 从子到父 父类引用指向子类对象 向下转型 从父到子 父类引用转为子类对象 /* 多态的弊端: 不能使用子类的特有功能。
我就想使用子类的特有功能?行不行? 行。
怎么用呢? A:创建子类对象调用方法即可。(可以,但是很多时候不合理。而且,太占内存了) B:把父类的引用强制转换为子类的引用。(向下转型)
对象间的转型问题: 向上转型: Fu f = new Zi(); 向下转型: Zi z = (Zi)f; //要求该f必须是能够转换为Zi的。 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | class Fu {
public void show() {
System.out.println( "show fu" );
}
}
class Zi extends Fu {
public void show() {
System.out.println( "show zi" );
}
public void method() {
System.out.println( "method zi" );
}
}
class DuoTaiDemo4 {
public static void main(String[] args) {
//测试
Fu f = new Zi();
f.show();
//f.method();
//创建子类对象
//Zi z = new Zi();
//z.show();
//z.method();
//你能够把子的对象赋值给父亲,那么我能不能把父的引用赋值给子的引用呢?
//如果可以,但是如下
Zi z = (Zi)f;
z.show();
z.method();
}
}
|
多态成员访问及转型的理解多态的问题理解: class 孔子爹 { public int age = 40;
public void teach() { System.out.println("讲解JavaSE"); } }
class 孔子 extends 孔子爹 { public int age = 20;
public void teach() { System.out.println("讲解论语"); }
public void playGame() { System.out.println("英雄联盟"); } }
//Java培训特别火,很多人来请孔子爹去讲课,这一天孔子爹被请走了 //但是还有人来请,就剩孔子在家,价格还挺高。孔子一想,我是不是可以考虑去呢? //然后就穿上爹的衣服,带上爹的眼睛,粘上爹的胡子。就开始装爹 //向上转型 孔子爹 k爹 = new 孔子(); //到人家那里去了 System.out.println(k爹.age); //40 k爹.teach(); //讲解论语 //k爹.playGame(); //这是儿子才能做的
//讲完了,下班回家了 //脱下爹的装备,换上自己的装备 //向下转型 孔子 k = (孔子) k爹; System.out.println(k.age); //20 k.teach(); //讲解论语 k.playGame(); //英雄联盟 多态内存图
多态中对象变化的内存图解
/* ClassCastException:类型转换异常 一般在多态的向下转型中容易出现 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class Animal {
public void eat(){}
}
class Dog extends Animal {
public void eat() {}
public void lookDoor() {
}
}
class Cat extends Animal {
public void eat() {
}
public void playGame() {
}
}
class DuoTaiDemo5 {
public static void main(String[] args) {
//内存中的是狗
Animal a = new Dog();
Dog d = (Dog)a;
//内存中是猫
a = new Cat();
Cat c = (Cat)a;
//内存中是猫
Dog dd = (Dog)a; //ClassCastException
}
|
案例练习
/* 多态练习:猫狗案例 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | class Animal {
public void eat(){
System.out.println( "吃饭" );
}
}
class Dog extends Animal {
public void eat() {
System.out.println( "狗吃肉" );
}
public void lookDoor() {
System.out.println( "狗看门" );
}
}
class Cat extends Animal {
public void eat() {
System.out.println( "猫吃鱼" );
}
public void playGame() {
System.out.println( "猫捉迷藏" );
}
}
class DuoTaiTest {
public static void main(String[] args) {
//定义为狗
Animal a = new Dog();
a.eat();
System.out.println( "--------------" );
//还原成狗
Dog d = (Dog)a;
d.eat();
d.lookDoor();
System.out.println( "--------------" );
//变成猫
a = new Cat();
a.eat();
System.out.println( "--------------" );
//还原成猫
Cat c = (Cat)a;
c.eat();
c.playGame();
System.out.println( "--------------" );
//演示错误的内容
//Dog dd = new Animal();
//Dog ddd = new Cat();
//ClassCastException
//Dog dd = (Dog)a;
}
}
|
不同地方饮食文化不同的案例 Person eat() SouthPerson eat() NorthPerson eat()
/* 不同地方饮食文化不同的案例 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | class Person {
public void eat() {
System.out.println( "吃饭" );
}
}
class SouthPerson extends Person {
public void eat() {
System.out.println( "炒菜,吃米饭" );
}
public void jingShang() {
System.out.println( "经商" );
}
}
class NorthPerson extends Person {
public void eat() {
System.out.println( "炖菜,吃馒头" );
}
public void yanJiu() {
System.out.println( "研究" );
}
}
class DuoTaiTest2 {
public static void main(String[] args) {
//测试
//南方人
Person p = new SouthPerson();
p.eat();
System.out.println( "-------------" );
SouthPerson sp = (SouthPerson)p;
sp.eat();
sp.jingShang();
System.out.println( "-------------" );
//北方人
p = new NorthPerson();
p.eat();
System.out.println( "-------------" );
NorthPerson np = (NorthPerson)p;
np.eat();
np.yanJiu();
}
}
|
|