[Core Java]C5 Inheritance

继承的宗旨(求同存异):

When defining a subclass by extending its superclass, you only need to indicate
the differences between the subclass and the superclass. When designing classes,
you place the most general methods into the superclass and more specialized
methods in its subclasses
. Factoring out common functionality by moving it to a
superclass is common in object-oriented programming.

在编译后的class文件中,不会有子类中没有重写的父类方法,该方法只存在于父类的class文件中
 

继承中的this与super

使用this的方法,this表示当前调用方法的对象的指针,注意,不是this在哪个类中声明就指向该类的对象

public void print() 
{     
   System.out.println(this.getClass());  
}    
public static void main(String[] args) {        
    Father fa = new Father(24,"lhz");        
    InheritedExp son =  new InheritedExp(22,"dxy");               
    fa.print();       
    son.print();    
}

class imp.one.Fatherclass
class imp.one.InheritedExp

使用super通常为了访问父类的方法或字段

super.method/super.field

但super并不属于引用类型,只是java内置的关键字,为了通知编译器调用的是父类的方法或字段

	public void print() {
		System.out.println(this);
		System.out.println(super);(error)
	}

继承中的重写

method signature:函数名+参数,不包括返回类型

在子类中重写方法要保证method signature一致

subclass对方法的重写对方法的可见度不可降低,superclass中的public方法在子类中重写不能声明为private,protected,default

abstract class

当继承一个虚基类可以有两种方式:

实现部分方法(可以全不实现,但不可以全实现),将当前类声明为abstract class

实现全部方法,当前类不再是abstract class

与实现一个Interface不同,实现一个接口必须实现它的所有方法

Object类

作为所有类的父类,从adt的相关类来说,array&collection都继承自object类

equal函数

equal函数在Object类中有声明,所以所有类都继承或是重写该方法

    public boolean equals(Object obj) {
        return (this == obj);
    }//未重写之前,只是判断引用的地址是否相等,所以如果是两个不同的对象,结果一定是false

对该方法进行重写时,要做一下操作

1.判断obj是否是当前类的对象或是子类对象(getClass或是instanceof,具体会在下文说明)

2.对选择的field进行判断

关于使用getClass或是instanceof,书中这样解释

• If subclasses can have their own notion of equality, then the symmetry
requirement forces you to use the getClass test.(对称性的要求)
• If the notion of equality is fixed in the superclass, then you can use the
instanceof test and allow objects of different subclasses to be equal to one
another(当比较的原则在父类中已经固定,想要两个不同的子类进行比较,比如AbstractSet有两个子类TreeSet和HashSet,当比较两个Set时,不管它实现方式是哪一种,比较的内容都在AbstractSet中已经确定)

HashCode函数

HashCode函数没重写之前,调用Object的HashCode函数,

public native int hashCode();(基于对象地址产生哈希值,即两个field一样的对象哈希值也不同)

两个对象Equal函数如果结果为True,hashcode的结果肯定相同,所以重写equal函数的同时也应该重写hashcode函数

hashcode结果相同,equal结果不一定相同

toString函数

通常为类名+field

当对象与String进行"+"操作时,编译器自动对object进行同String()操作

如果不重写toString方法,将调用Object中的toString()将打印对象所属类名和hashcode的16进制数
 

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

普通数组与ArrayList

java中普通数组可以不直接生命大小

int actualSize = . . .;
Employee[] staff = new Employee[actualSize];(可以在运行时动态再定义数组的大小)

对普通数组的读写用[i]操作,对ArrayList使用get和set

ArrayList作为数组的包装形式,可想而知ArrayList的效率是更低的,主要原因是对象要向上转型为Object,之后编译器还要向下转型(泛型在编译时的过程)

An ArrayList<Integer> is far less efficient than an int[] array because each value is separately
wrapped inside an object. You would only want to use this construct for small collections when
programmer convenience is more important than efficiency

猜你喜欢

转载自blog.csdn.net/weixin_41985660/article/details/82939729
C5
今日推荐