lhf.java /*super关键字:super关键字代表了父类空间的引用。super关键字的 作用:1. 子类父类存在着同名的成员时,在子类中默认是访问子类的成员,可以通过super关键字指定访问父类的成
/*
super关键字:
super关键字代表了父类空间的引用。
super关键字的 作用:
1. 子类父类存在着同名的成员时,在子类中默认是访问子类的成员,可以通过super关键字指定访问父类的成员。
2. 创建子类对象时,默认会先调用父类无参的构造方法,可以通过super关键字指定调用父类的构造方法。
super关键字调用父类构造方法要注意的事项:
1. 如果在子类的构造方法上没有指定调用父类的构造方法,那么java编译器会在子类的构造方法上面加上super()语句。
2. super关键字调用父类的构造函数时,该语句必须要是子类构造函数中的第一个语句。
3. super与this关键字不能同时出现在同一个构造函数中调用其他的构造函数。因为两个语句都需要第一个语句。
super关键字与this关键字的区别:
1. 代表的事物不一致。
1. super关键字代表的是父类空间的引用。
2. this关键字代表的是所属函数的调用者对象。
2. 使用前提不一致。
1. super关键字必须要有继承关系才能使用。
2. this关键字不需要存在继承关系也可使用。
3. 调用构造函数的区别:
1. super关键字是调用父类的构造函数。
2. this关键字是调用本类的构造函数。
*/
class Fu{
int x = 10;
String name;
public Fu(){
System.out.println("Fu类无参的构造方法..");
}
public Fu(String name){
this.name = name;
System.out.println("Fu类带参的构造方法..");
}
public void eat(){
System.out.println("小头爸爸吃番薯..");
}
}
class Zi extends Fu{
int x = 20;
int num;
public Zi(String name,int num){
super(name); //指定调用了父类带参的 构造方法...
this(); // 调用本类无参构造方法..
//super(); //指定调用了父类无参构造方法。。。
System.out.println("Zi类带参的构造方法..");
}
public Zi(){
System.out.println("Zi类无参的构造方法..");
}
public void print(){
System.out.println("x = " +super.x);
}
public void eat(){
System.out.println("大头儿子吃龙虾..");
}
}
class Demo9 {
public static void main(String[] args)
{
Zi z = new Zi("狗娃");
}
}
lhf1.java
/*
instanceof 关键字
instanceof关键字的作用:判断一个对象是否属于指定的类别。
instanceof关键字的使用前提:判断的对象与指定的类别必须要存在继承或者实现的关系。
instanceof关键字的使用格式:
对象 instanceof 类别
instanceof关键字的作用: 目前没用。但是后天我们学习 到了多态之后就非常有用。
一般我们做强制类型转换之前都会使用该关键字先判断一把,然后在进行转换的。
*/
class Animal{
String name;
String color;
public Animal(String name, String color){
this.name = name;
this.color = color;
}
}
//狗是属于动物中一种
class Dog extends Animal {
public Dog(String name,String color){
super(name,color); //指定调用父类两个 参数的构造函数。
}
public void bite(){
System.out.println(name+"咬人!!");
}
}
//老鼠 也是属于动物中一种
class Mouse extends Animal{
public Mouse(String name,String color){
super(name,color);
}
public void dig(){
System.out.println(name+"打洞..");
}
}
class Demo12{
public static void main(String[] args)
{
Dog d = new Dog("哈士奇","白色");
System.out.println("狗是狗类吗?"+ (d instanceof Dog));
System.out.println("狗是动物类吗?"+ (d instanceof Animal));
//System.out.println("狗是老鼠类吗?"+ (d instanceof Mouse)); // false true
Animal a = new Animal("狗娃","黄色"); //狗娃是人
System.out.println("动物都是狗吗?"+ (a instanceof Dog));
}
}
