1.Object类
Object类是所有Java类的根基类;
如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类。
piblic class Person{
}
等价于:
public class Person extends Object{
}
(1)toString方法:
a.Object类中定义有public.String toString()方法,其返回值是String类型,描述当前对象的有关信息。
b.在运行String与其它类型数据的连续操作时(如:System.out.println("info"+person)),将自动调用该对象类的toString()方法。
c.可以根据需要在用户自定义类型中重写toString()方法。
什么情况下要重写toString,object类里的toString只是把字符串的直接打印,数字的要转化成字符再打印,而对象,则直接打印该对象的hash码。所以当你要想按照你想要的格式去字符串一些对象的时候,就需要重写toString了。比如一个Student对象,直接toString肯定是一个hash码。然而你想得到的比如是:name:***,age:***。这时就重写toString就是在toString里写:System.out.println(“name:”+student.getName);
System.out.println(“age:”+student.getAge)。
1 class Dog{ 2 public String toString(){ //注意:重写toString方法 "public String toString()" 3 return "I'm a cool dog"; 4 } 5 } 6 public class Texttostring { 7 public static void main(String[] args){ 8 Dog d=new Dog(); 9 System.out.println("d:"+d); //System.out.println("d:"+d);等价于System.out.println("d:"+d.toString()); 10 } 11 12 }
输出结果:
d:I'm a cool dog
(2)equals方法:
(a)public boolean equals(Object obj)方法,提供定义对象是否相等的逻辑。
(b)Object的equals方法定义为:x.equals(y),当x和y是同一个对象的应用时返回true否则返回false。
(c)可以格局需要在用户自定义类型中重写object方法。
1 class Cat{ 2 private int color; 3 private int height; 4 private int weight; 5 Cat(int color,int height,int weight){ 6 this.color=color; 7 this.height=height; 8 this.weight=weight; 9 } 10 public boolean equals(Object obj){ 11 if(obj==null) return false; 12 else{ 13 if (obj instanceof Cat){ //判断obj是不是Cat对象的引用 14 Cat c=(Cat)obj; 15 if(c.color==this.color&&c.height==this.height&&c.weight==this.weight){ 16 return true; 17 } 18 } 19 } 20 return false; 21 } 22 } 23 24 public class TextEquals { 25 public static void main(String[] args){ 26 Cat c1=new Cat(1,1,2); 27 Cat c2=new Cat(1,1,2); 28 System.out.println(c1==c2); //比较c1,c2的引用 29 System.out.println(c1.equals(c2)); 30 String s1=new String("Hello"); 31 String s2=new String("Hello"); 32 System.out.println(s1==s2); //比较s1,s2的引用 33 System.out.println(s1.equals(s2)); 34 } 35 }
输出结果:
false
true
false
true
2.对象转型(casting):
(a)一个基类的引用类型变量可以“指向”其子类的对象。
(b)一个基类的引用不可以访问其子类对象新增加的成员(属性和方法)。
(c)可以使用变量instanceof类名,来判断该引用型变量所“指向”的对象是否属于该类或该类的子类。
(d)子类的对象可以当作基类的对象来使用称作向上转型(upcasting),反之称为向下转型(downcasting)。
1 class Animal{ 2 public String name; 3 Animal(String name){ 4 this.name=name; 5 } 6 } 7 class Cat extends Animal{ 8 public String eyecolor; 9 Cat(String name,String eyecolor){ 10 super(name); 11 this.eyecolor=eyecolor; 12 } 13 } 14 class Dog extends Animal{ 15 public String furcolor; 16 Dog(String name,String furcolor){ 17 super(name); 18 this.furcolor=furcolor; 19 } 20 } 21 public class Texttext { 22 public static void main(String[] args){ 23 Texttext text=new Texttext(); 24 Animal a=new Animal("name"); 25 Cat c=new Cat("catname","blue"); 26 Dog d=new Dog("dogname","black"); 27 text.f(a); 28 text.f(c); 29 text.f(d); 30 } 31 32 public void f(Animal a){ 33 System.out.println("name:"+a.name); 34 if(a instanceof Cat){ 35 Cat cat=(Cat)a; 36 System.out.println(cat.eyecolor+"\t"+"eyes"); 37 } 38 else if(a instanceof Dog){ 39 Dog dog=(Dog)a; 40 System.out.println(dog.furcolor+"\t"+"fur"); 41 42 } 43 } 44 } 45
输出结果:
name:name
name:catname
blue eyes
name:dogname
black fur
3.动态绑定与多态
(a)动态绑定是指在执行期间(而非编译期)判断所引用对象的实际类型,根据其实际的类型调用相应的方法。
(b)多态的存在有3个必要条件:1.要有继承;2.要有重写;3父类对象指向子类对象。
1 class Animal{ 2 public String name; 3 Animal(String name){ 4 this.name=name; 5 } 6 public void enjoy(){ 7 System.out.println("叫声....."); 8 } 9 } 10 class Dog extends Animal{ 11 private String furcolor; 12 Dog(String name,String furcolor){ 13 super(name); 14 this.furcolor=furcolor; 15 } 16 public void enjoy(){ 17 System.out.println("狗叫声....."); 18 } 19 } 20 class Bird extends Animal{ 21 private String feathercolor; 22 Bird(String name,String feathercolor){ 23 super(name); 24 this.feathercolor=feathercolor; 25 } 26 public void enjoy(){ 27 System.out.println("鸟叫声......"); 28 } 29 30 } 31 class Cat extends Animal{ 32 private String eyecolor; 33 Cat(String name,String eyecolor){ 34 super(name); 35 this.eyecolor=eyecolor; 36 } 37 public void enjoy(){ 38 System.out.println("猫叫声...."); 39 } 40 41 } 42 class Lady extends Animal{ 43 private Animal mypet; 44 Lady(String name,Animal mypet){ 45 super(name); 46 this.mypet=mypet; 47 } 48 public void mypetenjoy(){ 49 mypet.enjoy(); 50 } 51 52 } 53 public class Textduotai { 54 public static void main(String[] args){ 55 Cat cat=new Cat("shabi","yellow"); 56 Dog dog=new Dog("dashabi","pink"); 57 Bird bird=new Bird("xiaoshabi","purple"); 58 Lady l1=new Lady("erbi",cat); 59 Lady l2=new Lady("daerbi",dog); 60 Lady l3=new Lady("xiaoerbi",bird); 61 l1.mypetenjoy(); 62 l2.mypetenjoy(); 63 l3.mypetenjoy(); 64 }
输出结果:
猫叫声....
狗叫声.....
鸟叫声.....
4.抽象类(abstract)
(a)用abstract关键字来修饰一个类时,这个类叫做抽象类;
用abstract来修饰一个方法时,这个方法叫做抽象方法。
(b)含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写。
(c)抽象类不能被实例化;
(d)抽象方法只需声明,而不需实现。
1 abstract class Animal{ 2 public String name; 3 Animal(String name){ 4 this.name=name; 5 } 6 public abstract void enjoy(); 7 } 8 class Dog extends Animal{ 9 private String color; 10 Dog(String name,String color){ 11 super(name); 12 this.color=color; 13 } 14 public void enjoy(){ 15 System.out.println("猫叫声...."); 16 } 17 } 18 public class TextAbstract { 19 public static void main(String[] args){ 20 // Animal a=new Animal(); 错误! 抽象类不能被实例化; 21 Dog dog=new Dog("abc","yellow"); 22 dog.enjoy(); 23 } 24 }
输出结果:
猫叫声....
5.Final关键字:
(1)final的变量的值不能够被改变。
(a)final的成员变量; public void m(final int j) //j的值不能在方法中赋值。
(b)final的局部变量(形参); final int i=0; //i的值不能改变。
(2)final的方法不能够被重写。 public final void m(){}; //m方法不能被重写。
(3)final的类不能够被继承。 final clas T{} // T不能被继承。
6.接口