11.13 this 关键字
1.this关键字
package cn.dede.w; class A { private B b=null; public void fun() { this.b=new B(this);//当前值赋值给b this.b.fun();//到23 } public void print() { System.out.println("hi xn"); } } class B { private A a=null; public B(A a) { this.a=a; } public void fun() { this.a.print();//到18 } } public class ClassAB { public static void main(String args[]) { new A().fun();//到第2行 } }
2. 父类初级
package cn.dede.w; abstract class A1 { public abstract void fun(); } class B1 extends A1 { public void fun() { System.out.println("hi xn"); } } public class FirstAbstract { public static void main(String agrs[]) { B1 b=new B1();// b.fun(); } }
3.父类中级
package cn.dede.w; abstract class A2 { private String name; public A2(String name) { this.name=name; } public String getName() { return this.name; } public abstract void fun(); } class B2 extends A2{ public B2(String name) { super(name); } public void fun() { System.out.println(super.getName()); } } public class AbstractDemo1 { public static void main(String args[]) { B2 b=new B2("xn" ); b.fun(); } }
4.
package cn.dede.w; public abstract class PersonA1 { private String name; private int age; private String songs; 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 String getSongs() { return songs; } public void setSongs(String songs) { this.songs=songs; } public PersonA1(String name,int age, String songs) { this.name=name; this.age=age; this.songs=songs; } public PersonA1() { super();//作为一个特殊的关键字super表示调用父类的构造函数;它有动态类型,就是当前这个对象的类型。你可以对它调用成员函数,把它传递给别的函数,等等等等,静态地选择父类的那个版本。 } public String toString() { return "Person[name="+ name +",age="+ age +",songs="+ songs +"]"; } public abstract String speak(); }
package cn.dede.w; public class PersonStudent extends PersonA1 { private float score; public PersonStudent(String name,int age,String songs,float score) { super(name,age,songs); this.setScore(score); } public String speak() { return "身份标签-》学生; 我叫:"+super.getName()+" "+super.getAge()+"岁"+super.getSongs()+" "+this.score+"分"; } public float getScore() { return score; } public void setScore(float score) { this.score=score; } }
package cn.dede.w; public class PersonWorker extends PersonA1 { private float salary; public PersonWorker(String name,int age,String songs,float salary) { super(name,age,songs); this.setSalary(salary); } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary=salary; } public String speak() { return"工人说 -->我叫:"+super.getName()+" "+super.getAge()+"岁 "+super.getSongs()+" "+this.salary+"元" ; } }
package cn.dede.w; public class AbstractDemo2 { public static void main(String[]args) { PersonA1 student=new PersonStudent("仙女",13,"崂山道士",99f); PersonA1 worker=new PersonWorker("刘全有",33,"崂山道士",9999f); print(student.speak()); print(worker.speak()); } private static void print(String speak) { System.out.println(speak); } }
5.
抽象类练习
package cn.dede.w; abstract class A3 { public abstract void fun(); } class B3 extends A3 { public void fun() { System.out.println("yeah"); } } public class AbstractDemo3 { public static void main(String args[]) { A3 a=new B3(); a.fun(); } }