动手动脑

【extends】



调用时先调用父类的,后调用子类的构造函数原因:(子类拥有父的成员变量和成员方法,如果不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化;)
super();调用当前函数父类的构造函数,必须位于当前构造函数的首行。
【final】创建不可变类,该类
属性不可更改
不可从此类派生出新类
不可覆盖
且final变量必须初始化
用例——public final class A    , public final int a

【toString()函数】

public class ExplorationJDKSource {

    /**
     * @param args
     */
    public static void main(String[] args) {
A a=new A(); System.out.println(
a);//自动调用object的toString()函数返回类名@inter.to.hexstring(hashcode());

    }

}

class A{}
[运行结果]A@1h35t6y789


【扩展】
当任何一个String类型的量和对象用“+”连接时会调用toString()函数
class Fruit{
public static void main(String args[])
 {
  Fruit f=new Fruit();
  System.out.println("f="+f);
}
}
【运行结果】f=Fruit@1h35t6y789
若要使toString()函数有意义可以在子类中重新定义覆盖object()的函数,
public String toString()//覆盖则返回值类型,函数名都一样
{
return “这是一个覆盖父类toString()的函数”;
}

【super用法】

【调用父类函数】
public
class Fruit { public String toString() { return super.toString() ;//使用super.函数();调用父类的被覆盖的函数。这里调用的是object的toString()函数 //return "Fruit toString"; } public static void main(String[] args) { Fruit f=new Fruit(); System.out.println("f="+f); //System.out.println("f="+f.toString()); } }
【运行结果】f=lianxi.Fruit@15db9742
【调用父类变量】
public
class Fruit { public static void main(String[] args) { subclass s=new subclass(); } } class bassclass { public int a=4; } class subclass extends bassclass { public int a=8; subclass() {
System.out.print(super.a);
//System.out.print(
super.a);使用super.a调用bassclass()的a } }
【运行结果】
8 (子类a覆盖了父类的4)
// 4 (super.a调用bassclass()的a)

 【instanceof运算符】(子类对象可以直接赋值给父类对象;父类赋值给子类需要转换为指定类型((类型)对象),用(对象 instanceof 类型名)函数的返回值判断对象能否转换为指定类型(true false))

public static void main(String[] args) {
        
    Object hello="hello";//Object 是所有类的父类,所以所有类型都可赋值给 Object 类的对象(Object 类的实际类型是 String类型)
    System.out.println("字符串是否是Object的实例"+(hello instanceof Object));//返回值true  
System.out.println("字符串是否是String的实例"+hello instanceof String);//返回值true
System.out.println("字符串是否是math的实例"+(hello instanceof Math));//返回值false (声明hello时用的是Object所以hello的实际类型是Object)
System.out.println("字符串是否是math的实例"+("a" instanceof Math));//编译错误 因为String类既不是Math类,也不是Math类的父类,所以代码编译无法通过
}

}
【实例】
class
Mammal{} class Dog extends Mammal {} class Cat extends Mammal{} public class TestCast { public static void main(String args[]) { Mammal m=NULL; Dog d=new Dog(); Cat c=new Cat(); m=d; //d=m;编译错误,父类对象不能直接赋值给子类
d=(Dog)m; d=c; //c=(Cat)m;编译正确,运行时错误,Exception in thread "main" java.lang.ClassCastException: lianxi.Dog cannot be cast to lianxi.Cat
 at lianxi.Fruit.main(Fruit.java:14)
} }

 【继承(extends) 抽象(abstract)】

【实例】
package
lianxi; public class Zoo { public static void main(String args[]) { Feeder a=new Feeder("小李"); animal[] b=new animal[10];//用数组的形式初始化animal[]; for(int i=0;i<3;i++) b[i]=new Lion();//Lion是animal的子类所以Lion 对象可赋值给animal对象 for(int i=3;i<8;i++) b[i]=new Cat(); for(int i=8;i<10;i++) b[i]=new Monkey(); a.feedanimal(b, 10);//调用feeder类中的函数 } } class Feeder//饲养员类 { public String name; Feeder(String name) { this.name=name; } void feedanimal(animal[] an,int n) { for(int i=0;i<n;i++) System.out.println(an[i].eat()); } } abstract class animal//抽象类 animal abstract { public abstract String eat();//抽象方法 eat() } class Cat extends animal//cat extends animal { public String eat()//在子类中实例化抽象方法 { return "饲养猫"; } } class Monkey extends animal { public String eat() { return "饲养猴子"; } } class Lion extends animal { public String eat() { return "饲养狮子"; } }
【运行结果】
饲养狮子
饲养狮子
饲养狮子
饲养猫
饲养猫
饲养猫
饲养猫
饲养猫
饲养猴子
饲养猴子

 【接口 interface implenments 】

public class Zoo implements Food//implenments 实现接口,且必须在该类中实例化
{

    public static void main(String args[])
{
        
}
    public void Cook()//在该类中实例化
    {
        
    }
    
}
interface Food//interface 定义接口(在类外)
    {
        public void Cook();
    }
原文地址:https://www.cnblogs.com/zql98/p/9890276.html