package com.klvchen.java5;
import java.util.Random;
class Animal{
protected void eat() {
System.out.println("animal eat food");
}
}
class Cat extends Animal{
protected void eat() {
System.out.println("cat eat fish");
}
}
class Dog extends Animal{
public void eat() {
System.out.println("dog eat bone");
}
}
class Sheep extends Animal{
public void eat() {
System.out.println("sheep eat grass");
}
}
public class InterviewTest {
public static Animal getInstance(int key) {
switch(key) {
case 0:
return new Cat();
case 1:
return new Dog();
default:
return new Sheep();
}
}
public static void main(String[] args) {
int key = new Random().nextInt(3);
System.out.println(key);
Animal animal = getInstance(key);
animal.eat();
}
}
Person.java
package com.klvchen.java;
public class Person {
String name;
int age;
int id = 1001;
public void eat() {
System.out.println("人,吃饭");
}
public void walk() {
System.out.println("人,走路");
}
}
Woman.java
package com.klvchen.java;
public class Woman extends Person {
boolean isBeauty;
public void goShopping() {
System.out.println("女人喜欢购物");
}
public void eat() {
System.out.println("女人少吃,为了减肥");
}
public void walk() {
System.out.println("女人窈窕地走路");
}
}
Man.java
package com.klvchen.java;
public class Man extends Person{
boolean isSmoking;
int id = 1002;
public void earnMoney() {
System.out.println("男人负责挣钱养家");
}
public void eat() {
System.out.println("男人多吃肉,长肌肉");
}
public void walk() {
System.out.println("男人霸气的走路");
}
}
PersonTest.java
package com.klvchen.java;
import java.util.Date;
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Man();
p1.name = "Tom";
//不能调用子类所特有的方法、属性:编译时,p1 是 Person类型。
// p1.earnMoney();
// p1.isSmoking = true;
//有了对象的多态性以后,内存中实际上是加载了子类特有的属性和方法的,但是由于变量声明为父类类型,导致
//编译时,只能调用父类中声明的属性和方法。子类特有的属性和方法不能调用。
//如何才能调用子类持有的属性和方法?
Man m1 = (Man)p1;
m1.earnMoney();
m1.isSmoking = true;
//使用强转时,可能会出现 ClassCastException 的异常
// Woman w1 = (Woman)p1;
// w1.goShopping();
/*
* instanceof 关键字的使用
* a instanceof A: 判断对象 a 是否是类A的实例。如果是,返回 true,返回不是, 返回 false。
*
* 使用情境:为了避免在向下转型时出现 ClassCastException 的异常,我们在向下转型之前,先
* 进行 instanceof 的判断,一旦返回true,就进行向下转型。如果返回false,不进行向下转型。
*/
if( p1 instanceof Woman) {
Woman w1 = (Woman)p1;
w1.goShopping();
System.out.println("*********** Woman ***********");
}
if(p1 instanceof Man) {
Man m2 = (Man)p1;
m2.earnMoney();
System.out.println("*********** Man ***********");
}
if(p1 instanceof Person) {
System.out.println("*********** Person ***********");
}
if(p1 instanceof Object) {
System.out.println("*********** Object ***********");
}
//问题一:编译通过,运行时不通过
// Person p2 = new Woman();
// Man m2 = (Man)p2;
// Person p3 = new Person();
// Man m3 = (Man)p3;
// m3.earnMoney();
//问题二: 编译通过,运行时也通过
Object obj = new Woman();
Person p = (Person)obj;
//问题三: 编译不通过
// Man m4 = new Woman();
// Object o = new Date();
// String str1 = (String)o;
}
}
AnimalTest.java
package com.klvchen.java;
public class AnimalTest {
public static void main(String[] args) {
AnimalTest test = new AnimalTest();
test.func(new Dog());
System.out.println("*************************************");
test.func(new Cat());
}
public void func(Animal animal) {// Animal animal = new Dog();
animal.eat();
animal.shout();
if(animal instanceof Dog) {
Dog d = (Dog)animal;
d.watchDoor();
}
}
}
class Animal{
public void eat() {
System.out.println("动物, 进食");
}
public void shout() {
System.out.println("动物, 叫");
}
}
class Dog extends Animal{
public void eat() {
System.out.println("狗吃骨头");
}
public void shout() {
System.out.println("汪!汪!汪!");
}
public void watchDoor() {
System.out.println("看门");
}
}
class Cat extends Animal{
public void eat() {
System.out.println("猫吃鱼");
}
public void shout() {
System.out.println("喵!喵!喵!");
}
}
原文:https://www.cnblogs.com/klvchen/p/14436221.html