第37集:面向对象10_this隐式参数、内存分析
this和super都是Java每一个方法的隐式参数。
-
this关键字
普通方法中,this总是指向调用该方法的对象。
构造方法中,this总是指向正要初始化的对象。
-
this最常的用法:
让类中的一个方法,访问该类的另一个方法或属性。【setter和getter方法里用的多】
使用this关键字调用重载构造方法。避免相同的初始化代码,只能在构造方法中用,并且必须位于构造方法的第一句。
-
this使用时的注意事项:
this不能用于static方法!(this指向当前对象,static方法跟对象没有一毛钱的关系)
package com.test037;public class Student { String name; int id; public Student(String name,int id){ this(name);//通过this调用其他构造方法,必须位于第一句! this.name = name; this.id = id; } public Student(String name){ this.name = name; } public Student(){ System.out.println("构造一个对象"); } public void setName(String name){ this.name = name; //让类中的一个方法,访问该类的另一个方法或属性。 } public void study(){ this.name = "张三"; System.out.println(name+"在学习"); } public void sayHello(String sname){ System.out.println(name+"向"+sname+"说:你好!"); } }
第38集:面向对象(11)继承_基本概念
-
为什么需要继承?继承的作用?
第一好处:继承的本质在于抽象。类是对对象的抽象,继承是对某一批类的抽象。
第二好处:为了提高代码的复用性。
extands的意思是“扩展”。子类是父类的扩展。【注:JAVA中类只有单继承没有多继承! 接口可以多继承!】
继承介绍:现实世界中的继承无处不在。比如:动物:哺乳动物、爬行动物
哺乳动物:灵长目、鲸目等。继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
-
如何实现继承?使用extends关键字即可。
package test038;public class Animal {String eye;public void run(){ System.out.println("跑跑!");}public void eat(){ System.out.println("吃吃!");}public void sleep(){ System.out.println("zzzzz");}public Animal(){ System.out.println("创建一个动物!");}}
package test038;public class Mammal extends Animal { public void taisheng(){ System.out.println("我是胎生"); }}
package test038;public class TestExtends { public static void main(String[] args){ Mammal b = new Mammal(); b.run(); }}
第39集:面向对象(12)继承、方法的重写
-
方法的重写(override)
在子类中可以根据需要对从基类中继承来的方法进行重写。
重写方法必须和被重写方法具有相同方法名称、参数列表和返回类型。
重写方法不能使用比被重写方法更严格的访问权限。(由于多态)
方法重写和方法重载(over)的区别:很多人问重写和重载什么区别? 他们两个完全是两回事。除了名字都带一个“重”字之外。方法重载指的是:同一个类中,一个方法名对应了多个方法(形参列表不同)。方法的重写指的是:子类重写了父类的方法!
第40集:面向对象(13)继承Object类用法toString和equals方法重写toString
-
Object类
Object类是所有Java类的根基类
-
如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类
public class Person { ...}//等价于public class Person extends Object { ...}
-
toString方法
Object类中定义有public String toString()方法,其返回值是 String 类型,描述当前对象的有关信息。
在进行String与其它类型数据的连接操作时(如:System.out.println(“info”+person)),将自动调用该对象类的 toString()方法
-
Object类中toString方法的源代码是:
public String to String(){ return getClass().getName()+"@"+integer.toHexString(hashCode());}//默认是打印:类名+哈希吗(根据对象地址计算得出)
可以根据需要在用户自定义类型中重写toString()方法。比如String类就重写了toString方法,源代码如下:
public String toString(){ return this;}
Object中的equals方法可以当等号用,但很多类重写了equals方法。
package com.test040;public class Mobile {public String toString(){ return "我是电话";}}
package com.test040;public class TestObject { public static void main(String[] args){ Object obj = new Object(); Object obj2 = new Object(); System.out.println(obj.toString()); System.out.println(obj2.toString()); System.out.println(obj==obj2); System.out.println(obj.equals(obj2)); String str; Mobile m = new Mobile(); System.out.println(m.toString()); }}
第41集:面向对象(14)继承、super、构造器的调用、继承的内存分析(非常重要)
super是直接父类对象的引用。可以通过super来访问父类中被子类覆盖的方法或属性。
除了Object类之外其他所有类的构造方法第一句总是super(…)
任何类的构造方法中,若是构造方法的第一行代码没有显式的调用super(...);那么Java默认都会调用super(); 所以你这里的super(); 加不加都无所谓。
-
内存分析(重要!)
package com.test041;public class Animal { String eye; public void run(){ System.out.println("跑跑!"); } public void eat(){ System.out.println("吃吃!"); } public void sleep(){ System.out.println("ZZZ"); } public Animal(){ super(); System.out.println("创建一个动物!"); } }
package com.test041;public class Mammal extends Animal{ public void run(){ } public Mammal(){ System.out.println("创建一个Mammal"); }}
package com.test041;public class TestSuper { public static void main(String[] args){ Mammal a = new Mammal(); }}
第42集:面向对象(15)继承_组合
-
继承和组合
共同点:都可以实现代码的复用
-
不同点:
is-a关系:使用继承
has-a关系:使用组合
-
【使用组合就是把别的类当自个的成员变量】
package com.test042;public class Animal2 { String eye; public void run(){ System.out.println("run!"); } public void eat(){ System.out.println("eat!"); } public void sleep(){ System.out.println("zzzzz"); } public Animal2(){ super(); System.out.println("creat an animal"); } public static void main(String[] args){ Bird b = new Bird(); b.run(); b.animal2.eat(); }}
package com.test042;public class Bird { String name; Animal2 animal2 = new Animal2(); public void run(){ }}