📄 animal.java
字号:
/*
*子类当中的构造函数 先调用的父类的构造函数
*再调用子类的构造函数。
*
*在子类的构造函数中,当父类构造函数中没有不带参数的
*构造函数。。在子类中不带参数的构造函数中。。
*必须先调用父类的带参数的构造方法。
*/
/* 多态性理解
*
*当我们将子类对象的引用传给声明为父类的一个对象时,
*如果子类有的方法,就调用子类的
*这个方法,如果是子类没有的方法,就调用父类的方法。
*
*
*/
/* instanceof操作符
*用来判断一个对象是否是一个类的实例。
*
*子类的一个实例是父类的实例。。。。。
*
*当将子类的一个实例赋给次的实例时。
*些父的实例同时也是子类的一个实例了。。见下面的测试程序。
*
*/
class Animal
{
int height,weight;
public Animal()
{
}
void eat()
{
System.out.println("animal eat");
}
void sleep()
{
System.out.println("animal sleep");
}
void breathe()
{
System.out.println("animal breathe");
}
}
class Fish extends Animal
{
void breathe()
{
super.breathe();
System.out.println("fish breathe");
}
}
class Integeration
{
public static void main(String[] args)
{
/*
Animal an = new Animal();
Fish f = new Fish();
//an.breathe();
f.height = 30;
f.breathe();*/
Animal an = new Animal();
Fish fh = new Fish();
an = fh;
/*
if(an instanceof Animal)
{
System.out.println("an is animal's instance");
}
if(fh instanceof Fish)
{
System.out.println("fh is Fish's instance");
}*/
if(an instanceof Fish)
{
System.out.println("an is Fish's instance");
}
else
{
System.out.println("an isn't Fish's instance");
}
if(fh instanceof Animal)
{
System.out.println("fh is Animal's instance");
}
else
{
System.out.println("fh isn't Animal's instance");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -