P352-P354 ==和equals区别
package com.work; public class Homework03 { public static void main(String[] args) { Doctor do1 = new Doctor("L",99,"娅",'女',888.0); Doctor do2 = new Doctor("L",99,"娅",'女',888.0); System.out.println(do1.equals(do2)); } } class Doctor{ private String name; private int age; private String job; private char gender; private double sal; public Doctor(String name, int age, String job, char gender, double sal) { this.name = name; this.age = age; this.job = job; this.gender = gender; this.sal = sal; } public boolean equals(Object obj){ if(this == obj){//两个是否为一个对象 return true; } if(!(obj instanceof Doctor) ) {//不是这个类 return false; } //向下转型,因为obj的运行是Doctor 或者子类 Doctor doo = (Doctor) obj; return this.name.equals(doo.name) && this.age==doo.age && this.gender==doo.gender && this.job.equals(doo.job) && this.sal ==doo.sal; } //getset方法省略 }
P352作业11
-
Person p = new Student;//向上转型,p调用方法从子类开始找,父类的引用指向子类对象 Student s= (Person) p;//向下转型 p可以调用run,输出student run ,调用eat,person eat(找运行,运行无找父类) s可以调用run,输出student run,调用eat,person eat,调用study ,输出...
P354 ==和equals区别面试会考
-
P355作业13内容多
package com.work; public class Homework03 { public static void main(String[] args) { //多态数组 Person [] parr=new Person[4]; parr[0]=new Student("s1",'女',1,001); parr[1]=new Student("s2",'女',2,002); parr[2]=new Teacher("t1",'女',33,10); parr[3]=new Teacher("t2",'女',44,20); } public void bubble(Person [] parr) { Person temp=null; for(int i=0;i< parr.length-1;i++){ for(int j=0;j< parr.length-1-i;j++) { if(parr[j].getAge() P357
A类(注意隐藏的无参构造器)
haha b类有参数构造器
c类有参构造器
c类无参构造器哦
-
P358多态的总结
--
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)