package com.addd; //多态 public class Sld { private String name = "zhangsan"; public Sld() { aaa();//方法里面可放方法。 User(name); } public void aaa() { System.out.println("父类1号" + name); } public void User(String a) { System.out.println("父类2号" + name); } } class Dd extends Sld { private String name = "son"; public Dd() { aaa(); User(name); } public void aaa() { System.out.println("子类1号" + name);// 子类重写了父类的方法,父类就不会用了,就是子类用。所以。son } public void User(String name) { System.out.println("子类2号 :" + name); } } package com.addd; public class Zl { public static void main(String[] args) { //Sld s=new Dd(); //s.aaa(); Sld m=new Dd(); } } 子类1号null 子类2号 :zhangsan 子类1号son 子类2号 :son
总结:多态。方法,好重要