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));
}
}