10.23

package dongshou3;
public class Zoo1 {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  Feeder f = new Feeder("小李");
  // 饲养员小李喂养一只狮子
  f.feedLion(new Lion());
  // 饲养员小李喂养十只猴子
  for (int i = 0; i < 10; i++)
   {
   f.feedMonkey(new Monkey());
  }
  
  // 饲养员小李喂养5只鸽子
  for (int i = 0; i < 5; i++)
   {
   f.feedPigeon(new Pigeon());
  }
 
 }
}

class Feeder
{

 public String name;

 public Feeder(String name)
 {
  this.name = name;
 }
 
 public void feedLion(Lion l)
 {
  l.eat();
 }
 
 public void feedPigeon(Pigeon p)
 {
  p.eat();
 }
 
 public void feedMonkey(Monkey m)
 {
  m.eat();
 }
}

class Lion
{

 public void eat()
 {
  System.out.println("我不吃肉谁敢吃肉!");
 }
}

class Monkey
{
 public void eat()
 {
  System.out.println("我什么都吃,尤其喜欢香蕉。");
 }
}

class Pigeon
{

 public void eat()
 {
  System.out.println("我要减肥,所以每天只吃一点大米。");
 }
 
}
以上代码运行结果为:
我不吃肉谁敢吃肉!
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
 
package dongshou3;
public class ExplorationJDKSource {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  System.out.println(new A());
 }
}
class A{}
其运行结果为:
dongshou3.A@7dc5e7b4
 
package dongshou3;
public class Fruit {
 public String toString()
 {
  return "Fruit toString.";
 }
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  Fruit f=new Fruit();
  System.out.println("f="+f);
 // System.out.println("f="+f.toString());
 }
}
其运行结果为:
f=Fruit toString.
 
package dongshou3;
public class ParentChildTest {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  Parent parent=new Parent();
  parent.printValue();
  Child child=new Child();
  child.printValue();
  
  parent=child;
  parent.printValue();
  
  parent.myValue++;
  parent.printValue();
  
  ((Child)parent).myValue++;
  parent.printValue();
  
 }
 }
class Parent{
 public int myValue=100;
 public void printValue() {
  System.out.println("Parent.printValue(),myValue="+myValue);
 }
}
class Child extends Parent{
 public int myValue=200;
 public void printValue() {
  System.out.println("Child.printValue(),myValue="+myValue);
 }
}
 其运行的结果为:
Parent.printValue(),myValue=100
Child.printValue(),myValue=200
Child.printValue(),myValue=200
Child.printValue(),myValue=200
Child.printValue(),myValue=201
 

class Grandparent
{

    public Grandparent()
  {
         System.out.println("GrandParent Created.");
 
}

    public Grandparent(String string)
 {
         System.out.println("GrandParent Created.String:" + string);
 
 }
}
 
class Parent extends Grandparent
{

    public Parent()
  {
        //super("Hello.Grandparent.");
         System.out.println("Parent Created");
 
        //super("Hello.Grandparent.");
   }
}
 
class Child extends Parent
{

    public Child()
  {
 
        System.out.println("Child Created");
   }
}

public class TestInherits {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  Child c = new Child();
 }
}
其运行结果是:
GrandParent Created.
Parent Created
Child Created
 
package dongshou3;
public class TestInstanceof {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
    //但hello变量的实际类型是String
    Object hello = "Hello";
    //String是Object类的子类,所以返回true。
    System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
    //返回true。
    System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
    //返回false。
    System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
    //String实现了Comparable接口,所以返回true。
    System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
    String a = "Hello";
    //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
    //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));
 }
}
其运行结果是:
字符串是否是Object类的实例:true
字符串是否是String类的实例:true
字符串是否是Math类的实例:false
字符串是否是Comparable接口的实例:true
 
 
原文地址:https://www.cnblogs.com/cdl-sunshine/p/13881663.html