面向对象编程(三)
重写
1 在子类中可以根据需要对从基类中继承来的方法进行重新。
2 重写方法必须和被重写方法具有相同方法名称,参数列表和返回类型。
3 重写方法不能使用比重写方法更严格的访问权限
super关键字
This是当前对象的引用,super是当前父类对象的引用
super关键字,在Java类中使用super来引用基类的成分;eg 小块是栈,大块的是堆
继承中的构造方法
1 子类的构造过程中必须调用其基类的构造方法
2 子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法。
1)使用this(argument——list)调用本类的另外的构造方法
2)如果调用super,必须写在子类构造方法的第一行
3 如果子类的构造方法中没有显示地调用基类构造方法,则系统默认调用基类无参数的构造方法。
4 如果子类构造方法中既没有显示调用基类构造方法,而基类中又没有无参的构造方法,则编译出错。
class SuperClass {
private int n;
SuperClass() { //构造方法
System.out.println("SuperClass()");
}
SuperClass(int n) {
System.out.println("SuperClass(" + n + ")");
this.n = n;
}
}
class SubClass extends SuperClass {
private int n;
SubClass(int n) {
//子类构造方法中没有显示地调用基类构造方法,系统默认调用基类无参数的构造方法
System.out.println("Subclass(" + n + ")");
this.n = n;
}
SubClass() {
super(300);
System.out.println("SubClass()");
}
}
public class TestSuperSub {
public static void main(String arg[]) {
SubClass sc1 = new SubClass();
SubClass sc2 = new SubClass(400);
}
}
object类
Object类是所有Java类的根基类
如果在类的声明中未使用extends关键字指明其基类,则默认基类为object类
public class Person{
。。。
}
等价于
public class Person extends Object{
。。。
}
!Object类默认的equals方法是判断两个对象是否指向同一个东西,等价于==,所以要重写。
对象转换
分为向上转型,向下转型
对象转型
1 一个基类的引用类型变量可以“指向”其子类的对象
2 一个基类的引用不可以访问其子类对象新增加的成员(属性和方法)
3 可以使用 引用 变量 instanceof 类名 来判断该引用型变量所“指向”的对象是否属于该类或该类的子类。
4 子类的对象可以当作基类的对象来使用称作向上转型,反之称为向下转型
class Animal {
public String name;
Animal(String name) {
this.name = name;
}
}
class Cat extends Animal {
public String eyesColor;
Cat(String n,String c) {
super(n);
eyesColor = c;
}
}
class Dog extends Animal {
public String furColor;
Dog(String n,String c) {
super(n);
furColor = c;
}
}
publicclassTest {
publicstaticvoid main(String args[]){
Animal a = new Animal("name");
Cat c = new Cat("catname","blue");
Dog d = new Dog("dogname","black");
System.out.println(a instanceof Animal); //true
System.out.println(c instanceof Animal); //true
System.out.println(d instanceof Animal); //true
System.out.println(a instanceof Cat); //false
a = new Dog("bigyellow","yellow");
System.out.println(a.name); //bigyellow
System.out.println(a.furname); //!error
System.out.println(a instanceof Animal); //true
System.out.println(a instanceof Dog); //true
Dog d1 = (Dog)a; //要加强制符
System.out.println(d1.furColor); //yellow
}
}
父类引用指向子类对象,子类属性并不能访问,只能访问animal的部分
多态
动态绑定和多态
动态绑定是指“在执行期间(而非编译期)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法。
class Animal {
private String name;
Animal(String name) {
this.name = name;
}
public void enjoy() {
System.out.println("叫声。。。。。");
}
}
class Cat extends Animal {
private String eyesColor;
Cat(String n,String c) {
super(n);
eyesColor = c;
}
public void enjoy() {
System.out.println("猫叫声。。。");
}
}
class Dog extends Animal {
private String furColor;
Dog(String n,String c) {
super(n);
furColor = c;
}
public void enjoy() {
System.out.println("狗叫声。。。");
}
}
/*
class Bird extends Animal {
Bird() {
super("bird");
}
public void enjoy() {
System.out.println("niao叫声。。。");
}
}
*/
class Lady {
private String name;
private Animal pet;
Lady(String name, Animal pet) {
this.name = name;
this.pet = pet;
}
public void myPetEnjoy(){
pet.enjoy();
}
}
//动态绑定,多态,值绑定,最大化了可扩展性
public class TestPolymoph {
public static void main(String args[]){
Cat c = new Cat("catname","blue");
Dog d = new Dog("dogname","black");
//Bird b = new Bird();
Lady l1 = new Lady("l1",c);
Lady l2 = new Lady("l2",d);
//Lady l3 = new Lady("l3",b);
l1.myPetEnjoy();
l2.myPetEnjoy();
//l3.myPetEnjoy();
}
}
上面例子中,根据Lady对象的成员变量pet所引用的不同的实际类型二调用相应的enjoy方法
Tips:
1 要有继承
2 要有重写
3 父类引用指向子类对象
抽象类
1 用abstract关键字来修饰一个类时,这个类叫做抽象类;用abstract来修饰一个方法时,该
方法叫做抽象方法
2 含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写。
3 抽象类不能被实例化。
4 抽象方法只需声明,而不需实现。
abstract class Animal {
private String name;
Animal(String name) {
this.name = name;
}
public abstractn void enjoy(); //抽象方法就是为了重写
}
class Cat extends Animal {
private String eyesColor;
Cat(String n,String c) {
super(n);
eyesColor = c;
}
public void enjoy() {
System.out.println("xx");
}
}
当一个类里面有抽象方法时,这个类必须定义为抽象类,抽象类是残缺的。
final关键字(不可修改不可变化)
1 final的变量的值不能被改变,即变为常量
2 final的方法不能够被重写
3 final的类不能够被继承
4 final修饰属性,则该类的属性不会进行隐式的初始化,类的初始化属性必须有值,或在构造方法中赋值
接口
Java只支持单继承,接口可以实现多继承的现象
1 接口是抽象方法和常量值的定义的集合。
2 从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实习
public interface Runner {
public static final int id=1;
public void start();
public void run();
public void stop();
}
接口特性
1 接口可以多重实现
2 接口中声明的属性默认为public static final的;也只能是public static final的;
3 接口中只能定义抽象方法,而这些方法默认为public的,也只能是public的。
4 接口可以继承其他接口,并且添加新的属性和抽象方法。
接口
1 多个无关的类可以实现同一个接口
2 一个类可以实现多个无关的接口
3 与继承关系类似,接口与实现类之间存在多态性
interface Singer {
public void sing();
public void sleep();
}
interface Painter {
public void paint();
public void eat();
}
class Student implements Singer {
private String name;
Student(String name) {
this.name = name;
}
public void study(){
System.out.println("studying");
}
public String getName() {
return name;
}
public void sing() {
System.out.println("student is singing");
}
public void sleep() {
System.out.println("student is sleep");
}
}
class Teacher implements Singer,Painter {
private String name;
public String gettString(){
return name;
}
Teacher(String name){
this.name = name;
}
public void teach(){
System.out.println("teaching");
}
public void sing(){
System.out.println("teacher is singing");
}
public void sleep(){
System.out.println("teacher is sleeping");
}
public void paint(){
System.out.println("teaching is painting");
}
public void eat(){
System.out.println("teacher is eating");
}
}
public class TestInterface {
public static void main(String args[]){
Singer s1 = new Student("le");
s1.sing();
s1.sleep();
Singer s2 = new Teacher("steven");
s2.sing();
s2.sleep();
Painter p1 = (Painter)s2;
p1.paint();
p1.eat();
}
}
每一个接口暴露了这个类的一部分方法,使用一个接口,只能使用这个接口定义的方法。