java 09

学习内容
1.继承的应用
子类与父类
成员变量重名——间接调用、直接调用
2.代码如下:
(1)

package demo01;

public class fu {
int num= 100;
  public void methodfu(){
	 System.out.println(num);  
	 
  }
}

(2)

package demo01;

public class zi extends fu {
int num=200;
public void methodzi(){
	 System.out.println(num); 
}
}

(3)

package demo01;

public class fuziMain {
public static void main (String[] args) {
	zi zi=new zi();
	zi.methodfu();
	zi.methodzi();		
}
}

(4)

package demo02;

public class Fu {
int num=100;
public void methodfu() {
	 System.out.println("父类方法methodfu执行"); 
}
public void method(){
	 System.out.println("父类方法methodfu执行"); 
}
}

(5)

package demo02;

public class Zi extends Fu {
int num=200;
public void methodzi() {
	System.out.println("子类方法method执行");
}
public void method() {
	System.out.println("子类方法method执行");
}
}

(6)

package demo02;

public class Fuzimain {
  public static void main (String [] args) {
	  Zi zi =new Zi();
	  Fu fu=new Fu();
	  fu.methodfu();
	  zi.methodzi();
	  zi.methodfu();
	  fu.method();//创建的对象是谁,就优先用谁,没有就往上找
	  zi.method();//创建的对象是谁,就优先用谁,没有就往上找
  }
}

大数据2003彭莎

你可能感兴趣的:(java 09)