多态

public class Child extends Father{
int age = 10;
String name="abc";


void disp() {
System.out.println("Child");
}

}


public class Child2 extends Father{
void disp(){
System.out.println("AAAAA");
}

}


public class Father {
int age=30;


void disp(){
System.out.println("father");
}






@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Father other = (Father) obj;
if (age != other.age)
return false;
return true;
}


// @Override
// public boolean equals(Object arg0) {
// if (arg0 instanceof Father) {
// Father other = (Father) arg0;
// if (age == other.age) {
// return true;
// }
// }
// return false;
// }






}


public class MainTest {


public static void fun(){

}

// fun_int
public static void fun(int n){

}


public static void main(String[] args) {
/*----多态----
* 可实现代码的优化    减少耦合性   
* 方便代码的扩展

多态:相同的代码在运行时
    会根据不同的实际情况 执行不同的代码
    
静态多态: 函数的重载
  在预加载阶段(静态区)就已经被确定 
 
动态多态: 父引用引用子实例  函数的重写
直到程序运行到 实例方法的掉用时才确定


 
* */
// //父类引用引用子类实例
// Father obj1 = new Child();
// Father obj2 = new Child2();
//
// //若该实例有自己定义的方法 
// //  优先使用自己定义的方法
// //  方法看实例
// obj1.disp();
// obj2.disp();
//
// //  属性时 看引用
// System.out.println(obj1.name); 


/*--------- 面向对象的类型强转 (引用强转) ----------*/
// Father obj2 = new Child2();
// // 实例对象的引用  instanceof 类型(对象本身类型/父类)
// if (obj2 instanceof Father) {
//
// }
// //获取 引用的实例对象的  类型名
// System.out.println(obj2.getClass());
//
// //面向对象的类型强转  
// //  原始引用 和目标类型 必须有父类子类的关系
// // 子类->父类
// Child testObj = new Child();
// System.out.println (testObj.age);
// System.out.println (((Father)testObj).age);
//
// // 父类->子类     类型的降等
// // 父类引用子类实例  才能强转成 对应的类型
// //   否则运行异常
// Father testObj2 = new Child();
// Child test = (Child)testObj2;


/*------equals() 验证-----*/
Father testObj = new Father();
Object obj = new Object();
Father tmp = testObj;
Father tmp2 = new Father();
tmp2.age = -100;
Child tmp3 = new Child();


System.out.println( testObj.equals(tmp3));


}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值