P313多态属性无重写看编译类型,比较操作符,动态绑定机制,多态参数,equals-P325
P313多态属性类型无重写看编译类型
instanceOf 比较操作符
package com.Polymoraphic.Exercise; public class PolyExcercise { public static void main(String[] args) { Base base =new Sub(); //属性没有重写之说!属性的值看编译类型 System.out.println(base.count);//输出10 //instanceOf 比较操作符,用于判断对象的运行类型是否为XX 类型或XX 类型的子类型 System.out.println(base instanceof Base);//true,运行类型Sub,是Base子类 Base bb = new Base(); System.out.println(bb instanceof Base);//true } } class Base{ int count =10; } class Sub extends Base{ int count=20; }
练习1
--
//b===s,判断是b和s指向的对象的地址是否相同 //b.display()方法去找运行类型 //总结属性看编译类型,方法看运行类型
Java的动态绑定机制(非常重要)DynamicBindingP315
-
package com.Polymoraphic.Exercise; public class DynamicBinding { public static void main(String[] args) { //a 的编译类型A, 运行类型B A a = new B();//向上转型 System.out.println(a.sum());//?40 -> 30 System.out.println(a.sum1());//?30-> 20 //a的运行机制是B,调用方法的时候先找子类的方法,没有,继承机制再找父类的方法 //比如只有父类的sum()方法时候有getI()。因为调用a对象的sum()方法时候 // 该方法会和该对象的内存地址/运行类型绑定,极为调用的是子类的getI(),20+10=30 //属性没有动态绑定机制,哪里声明哪里调用 //比如只有父类的sum1()方法时候,return i + 10,i在父类=10,所以10+10=20 } } class A {//父类 public int i = 10; //动态绑定机制: public int sum() {//父类sum() return getI() + 10;//20 + 10 } public int sum1() {//父类sum1() return i + 10;//10 + 10 } public int getI() {//父类getI return i; } } class B extends A {//子类 public int i = 20; // public int sum() { // return i + 20; // } public int getI() {//子类getI() return i; } // public int sum1() { // return i + 10; // } }
多态的应用--数组PolyArrayP315
-
package com.Poly_polyer; public class Person{ private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public String Say(){ return "\t"+name+"\t"+age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.Poly_polyer; public class Student extends Person { private double score; public Student(int age, java.lang.String name, double score) { super(age, name); this.score = score; } //重写父类say方法; public String Say(){ return "Student"+super.Say()+"\t"+score; } public void student(){ System.out.println("学生 "+getName()+"正在学java"); } public double getScore() { return score; } public void setScore(double score) { this.score = score; } }
package com.Poly_polyer; public class Teacher extends Person{ private double salary; public Teacher(int age, String name, double salary) { super(age, name); this.salary = salary; } public String Say(){ return "Teacher" +super.Say()+"\t"+"salary"+salary; } public void teach(){//特有 System.out.println("teacher"+getName()+"正在讲课java "); } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
package com.Poly_polyer; public class PloyArray { public static void main(String[] args) { //父类的引用指向子类的对象 Person[] persons=new Person[5]; persons[0] = new Person(20,"ka"); persons[1] = new Student(20,"skb",101); persons[2] = new Student(22,"skc",102); persons[3] = new Teacher(99,"wwa",9999); persons[4] = new Teacher(90,"Bwa",6666); //循环遍历多态数组 for (int i = 0; i < persons.length; i++) { System.out.println(persons[i].Say());//动态绑定,看运行类型来调用 if(persons[i] instanceof Student){ Student students =(Student) persons[i]; students.student(); }else if(persons[i] instanceof Teacher){ ((Teacher) persons[i]).teach();//一条语句更简洁 }else{ } } } }
多态的应用--参数P318
-
package com.poly_parameter; public class Employee { private String name; private double msalary; public Employee(String name, double msalary) { this.name = name; this.msalary = msalary; } public double getAnnual(){ return msalary*12; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getMsalary() { return msalary; } public void setMsalary(double msalary) { this.msalary = msalary; } }
package com.poly_parameter; public class CustomEmployee extends Employee { public CustomEmployee(String name, double msalary) { super(name, msalary); } public void work(){ System.out.println("普通员工"+getName()+"在work..."); } public double getAnnual(){ return super.getAnnual();//可以直接调用父类方法 } }
package com.poly_parameter; public class MEmployee extends Employee{ private double bonus; public void manage(){ System.out.println("经理"+getName()+" 在manage"); } public double getAnnual(){ return super.getAnnual()+getBonus(); } public MEmployee(String name, double msalary, double bonus) { super(name, msalary); this.bonus = bonus; } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } }
package com.poly_parameter; public class PolyParameter { public static void main(String[] args) { Employee ca = new CustomEmployee("yue",1); Employee ma =new MEmployee("nyue",11,2); PolyParameter p=new PolyParameter(); p.ShowEmployee(ca);//运行类型CustomEmployee p.Testwork(ca); System.out.println("======="); p.ShowEmployee(ma); p.Testwork(ma); } public void ShowEmployee(Employee e) { System.out.println("annualsalary= " + e.getAnnual()); //动态绑定机制 } public void Testwork(Employee e) { if (e instanceof CustomEmployee) { ((CustomEmployee) e).work();//向下转型 } else if (e instanceof MEmployee) { ((MEmployee) e).manage(); } } }
P319 Object类详解
p319equals面试会用
----
察看jdk方法的源码 ctr+b,或者点击方法,右键,goto-D or U
package com.Object; public class Equals01 { public static void main(String[] args) { A a =new A(); A b=a; A c=b; System.out.println(a == c);//引用类型a指向A对象,bc也是,为true; B bobj = a;//向上转型 System.out.println(bobj == c);//true地址同 int n1=10; double n2=10.0; System.out.println(n1==n2);//基本数据类型,true System.out.println("hello".equals("abc"));; //String的equals方法,源码怎么看ctr+b,源代码非常的经典 //第一判断是否是同一引用对象 // 第二个判断是否为字符串或者子类,判断长度,判断每个位置字符是否相等 //object的equals方法默认比较对象地址是否相同(两个对象是否相同) //integer的equals方法,先判断是否是integer的子类或者integer类,然后判断值是否相等 Integer integer1 = new Integer(1000); Integer integer2 = new Integer(1000); System.out.println(integer1==integer2);//false,new了两次 System.out.println(integer1.equals(integer2));//true String str1 = new String("hss"); String str2 = new String("hss"); System.out.println(str1 == str2);//false,new了两次 System.out.println(str1.equals(str2));//true } } class A extends B{ } class B{ }
重写equals方法P322
package com.Object; public class EqualsExercise01 { public static void main(String[] args) { Person p1 = new Person("yy",20,'女'); Person p2 = new Person("xx",20,'女'); System.out.println(p1.equals(p2));//如果是没有重写,返回假, // p1,p2是object的子类,判断位置,两个对象 //重写之后,返回为真 } } class Person{ private String name; private int age; private char gender; public Person(String name, int age, char gender) { this.name = name; this.age = age; this.gender = gender; } public boolean equals(Object obj){ //判断两个对象是否为一个对象,是返回true if(this == obj){ return true; } //类型的判断 if(obj instanceof Person){//是Person,我们才能比较 Person p = (Person) obj;//进行向下的转行,获得obj的各个属性 return this.name.equals(p.name) && this.age==p.age && this.gender == p.gender; }//如果不是person,直接返回true return false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } }
String s1=new String("hsp"); String s2=new String("hsp"); system.out.println(s1==s2);//false,不是同一个对象 system.out.println(s1.equals(s2));//true,String重写了,先判断类型,再判断字符串
int a=9;//P324 double b=9f; System.out.println(a==b);//t,基本类型看值 char ch1 = 'A'; char ch2 = 12; System.out.println( 65== ch1);//T,AS码对应就是 System.out.println( 12 == ch2);//T,基本数据类型
P325hashcode
--