************************************
****面试题:****
java的的三大特性:
封装
继承
多态
************************************
==========================================================================================
4.多态的主板示例
6.Object类--toString()
多 态★★★★★
(面向对象特征之一)
:函数本身就具备多态性,某一种事物有不同的具体的体现。
体现:父类引用或者接口的引用指向了自己的子类对象。//Animal a = new Cat();
多态的好处:提高了程序的扩展性。
多态的弊端:当父类引用指向子类对象时,虽然提高了扩展性,但是只能访问父类中具备的方法,不可以访问子类中特有的方法。(前期不能使用后期产生的功能,即访问的局限性)
多态的前提:
1:必须要有关系,比如继承、或者实现。
2:通常会有覆盖操作。
==========================================================================================
a:向上转型
b:向下转型
class
Cat
extends
Animal
{
public
void
eat()
{
System.out.println(
"吃鱼"
);
}
public
void
catchMouse()
{
System.out.println(
"抓老鼠"
);
}
}
class
Dog
extends
Animal
{
public
void
eat()
{
System.out.println(
"吃骨头"
);
}
public
void
kanJia()
{
System.out.println(
"看家"
);
}
}
class
Pig
extends
Animal
{
public
void
eat()
{
System.out.println(
"饲料"
);
}
public
void
gongDi()
{
System.out.println(
"拱地"
);
}
}
//-----------------------------------------
class
DuoTaiDemo2
{
public
static
void
main(String[] args)
{
//Animal a = new Cat();//类型提升。 向上转型。
//a.eat();
/
/如果想要调用猫的特有方法时,如何操作?
//强制将父类的引用转成子类类型。向下转型。
///Cat c = (Cat)a;
//c.catchMouse();
//千万不要出现这样的操作,就是将父类对象转成子类类型。
//我们能转换的是父类引用指向了自己的子类对象时,该应用可以被提升,也可以被强制转换。
//多态自始至终都是子类对象在做着变化。
// Animal a = new Animal();
// Cat c = (Cat)a;
/*
毕姥爷 x = new 毕老师();
x.讲课();
毕老师 y = (毕老师)x;
y.看电影();
*/
function(new Dog());
function(new Cat());
}
public static void function(Animal a)//Animal a = new Cat();
{
a.eat();
/*
if(a instanceof Animal)
{
System.out.println("haha");
}
else
*/
if(a instanceof Cat)
{
Cat c = (Cat)a;
c.catchMouse();
}
else if(a instanceof Dog)
{
Dog c = (Dog)a;
c.kanJia();
}
/*
instanceof : 用于判断对象的类型。 对象 intanceof 类型(类类型 接口类型)
*/
}
}
2.多态---------转型
关键字
instanceof
用法:
result = object instanceof class
参数:
Result:布尔类型。
Object:必选项。任意对象表达式。
Class:必选项。任意已定义的对象类。
说明:
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
result = object instanceof class
参数:
Result:布尔类型。
Object:必选项。任意对象表达式。
Class:必选项。任意已定义的对象类。
说明:
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
3.多态中成员的特点---
面试中有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
class
F{
int
num =
5
;
//变量
public
void
method1(){
//成员函数
System.out.println(
"Fu method_1"
);
}
public
void
method2(){
System.out.println(
"Fu method_2"
);
}
static
void
method4(){
//静态成员函数
System.out.println(
"fu method_4"
);
}
}
class
Z
extends
F{
int
num =
8
;
public
void
method1(){
System.out.println(
"Zi menthod_1"
);
}
public
void
method3(){
System.out.println(
"Zi method_3"
);
}
static
void
method4(){
System.out.println(
"Zi method_4"
);
}
}
public
class
DuoTaiDemo {
public
static
void
main(String[] args){
F f =
new
Z();
Z z =
new
Z();
//第一种情况
f.method1();
f.method2();
// f.method3();//为啥编译错误
//第二中情况
System.out.println(f.num);
//打印的是5....面试的时候有
System.out.println(z.num);
//打印的是8
//第三种情况
f.method4();
//结果是Fu menthod_4
z.method4();
//结果是Zi menthod_4
|
/*
* 在多态中成员函数的特点:
* 在编译时期:参阅引用型变量所属的类中是否有调用的方法,如果有,编译通过,如果没有,编译失败
* 在运行时期:参阅对象所属的类中是否有调用的方法.
* 简单总结就是:
成员函数在多态调用时,编译看左边,运行看右边.
成员
变量
的特点:
无论编译和运行,都参考左边(引用型变量所属的类).
静态成员函数和变量
的特点:
无论编译和运行,都参考左边.
4.多态的主板示例
5.多态的扩展示例
6.Object类--equals()
class
Demo{
private
int
num;
Demo(
int
num){
this
.num = num;
}
public
boolean
equals(Object obj){
//重写
if
(!(obj
instanceof
Demo))
//
return
false
;
Demo d = (Demo)obj;
//向下转型
// return this.num == obj.num;//obj里面不存在num,要向下转型
return
this
.num == d.num;
}
}
class
Person{
//
}
public
class
ObjectDemo {
public
static
void
main(String[] args){
Demo d =
new
Demo(
4
);
Person p =
new
Person();
System.out.println(d.equals(p));
//编译没问题,但是运行会出ClassCastException..
//因为p无法强制转换成Demo类型
}
}
6.Object类--toString()
toString( )========》》就是getClass().getName() + '@' + Integer.toHexString(hashCode());;
解析如下代码:
1
2
3
|
System.out.println(z.hashCode()); //23965177
System.out.println(z.toString()); //day01.Zi@16dadf9
System.out.println(z.getClass()); //class day01.Zi
|