Java继承与多态的动手动脑

 





package z; public class Pet //定义基类
{ String name; int love; int health; public Pet()//无参的构造函数
{ System.out.println("调用Pet的构造函数"); } public Pet(String name,int love,int health)//有参的构造函数
{ this.name=name; this.love=love; this.health=health; } public void toEat() { System.out.println("吃东西"); } public void toplay() { System.out.println("玩耍"); } } package z; public class Monkey extends Pet { String name; int love; int health; String sex; public Monkey()//无参的构造函数
{ super(); System.out.println("调用monkey的构造函数"); } public Monkey(String name,int love,int health,String sex) { super(); this.sex=sex; } public void supertoEat()//使用super调用父类的 函数 { System.out.println("猴子爱吃桃子"); } public void toPlay() { System.out.println("猴子爱爬树"); } public static void main(String[] args) { // TODO Auto-generated method stub Monkey m=new Monkey(); m.toEat(); //super.toEat(); m.toPlay(); } }



&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

 

自我判断结果:第二句与第四句出错

第二句:父类对象不能直接对子类赋值

第四句:继承来自同一个基类的子类之间不能进行赋值

package z;

class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}

public class TestCast
{
	public static void main(String args[])
	{
		Mammal m;
		Dog d=new Dog();
		Cat c=new Cat();
		//m=d;
		//d=m;
		//d=(Dog)m;
		//d=c;
		//c=(Cat)m;

	}
}

  实际运行后的结论:最后一句也存在错误。原因第一句将m=d,后在执行最后一句相当于将d赋给c,等效于第四句故产生错误。

 


 1 package z;
 2 
 3 public class ParentChildTest {
 4     public static void main(String[] args) {
 5         Parent parent=new Parent();
 6         parent.printValue();
 7         Child child=new Child();
 8         child.printValue();
 9         
10         parent=child;
11         parent.printValue();
12         
13         parent.myValue++;
14         parent.printValue();
15         
16         ((Child)parent).myValue++;
17         parent.printValue();
18         
19     }
20 }
21 
22 class Parent{
23     public int myValue=100;
24     public void printValue() {
25         System.out.println("Parent.printValue(),myValue="+myValue);
26     }
27 }
28 class Child extends Parent{
29     public int myValue=200;
30     public void printValue() {
31         System.out.println("Child.printValue(),myValue="+myValue);
32     }
33 }

 1 package z;
 2 
 3 public class ParentChildTest {
 4     public static void main(String[] args) {
 5         Parent parent=new Parent();
 6         parent.printValue();
 7         Child child=new Child();
 8         child.printValue();
 9         
10         parent=child;
11         parent.printValue();
12         
13         parent.myValue++;
14         parent.printValue();
15         
16         ((Child)parent).myValue++;
17         parent.printValue();
18         
19     }
20 }
21 
22 class Parent{
23     public int myValue=100;
24     public void printValue() {
25         System.out.println("Parent.printValue(),myValue="+myValue);
26     }
27 }
28 class Child extends Parent{
29     public int myValue=200;
30     public void superprintValue() //将printValue改为superprintValue
31     {
32         System.out.println("Child.printValue(),myValue="+myValue);
33     }
34 }

j结论:当子类中的方法与基类方法同名时,要想调用基类中的方法需加super

 

  

posted @ 2017-11-09 22:06  小张在搬砖  阅读(174)  评论(0)    收藏  举报