面向对象的多态实现
package duotaixing; public class next { public static void main(String[] args) { // TODO Auto-generated method stub next a=new next(); a.put(new zoo()); a.put(new dog()); a.put(new cat()); } public void put(zoo a){///可以从子类到父类的识别 a.eat(); a.sleep(); if(a instanceof dog) {///如果long转化int一般 dog b=(dog)a; b.good(); } if(a instanceof cat) { cat b=(cat)a; b.good(); } } } class zoo{ String name; int age; public void eat() { System.out.println("动物进食"); } public void sleep() { System.out.println("动物睡觉"); } } class dog extends zoo{///继承父类的属性 public void eat() { System.out.println("狗进食"); } public void sleep() { System.out.println("狗睡觉"); } public void good() { System.out.println("狗会看家"); } } class cat extends zoo{ public void eat() { System.out.println("猫进食"); } public void sleep() { System.out.println("猫睡觉"); } public void good() { System.out.println("猫会看家"); } }
动物进食
动物睡觉
狗进食
狗睡觉
狗会看家
猫进食
猫睡觉
猫会看家
包装类的使用
package nie; import org.junit.Test; public class leixing { @Test public void test1() { int a1=10; String str1=a1+""; Integer a2=a1; String str2=String.valueOf(a2);///基本类型----》String类。。调用String类的重载valueOf String str3=String.valueOf(true); System.out.println(a2); System.out.println(str1); System.out.println(str2); System.out.println(str3); int a3= Integer.parseInt(str2);///同上 相反 System.out.println(a3); boolean b=Boolean.parseBoolean(str3); System.out.println(b); } }
10
10
10
true
10
true