One 许增宝

Java 中继承
package cn.onexzb.www; public class TestExtends { pu...
扫描右侧二维码阅读全文
27
2019/04

Java 中继承

package cn.onexzb.www;

public class TestExtends {
    public static void main(String[] args){
        Students a = new Students("许增宝", 18, "安徽理工大学");
        a.print();
    }
}

// 未继承过则默认继承 Object
class Person{
    String name;
    int age;
}

class Students extends Person{
    String add;
    Students(String name, int age, String add){
        super.name = name;
        this.age = age;
        this.add = add;
    }
    
    public void print(){
        System.out.println("我继承了父类 Person");
    }
    
}

 instanceof是二元运算符,左边是对象,右边是类;当对象是右面类或子类所创建对象时,返回true;否则,返回false。比如:

public class Test{
    public static void main(String[] args) {
        Student s = new Student("高淇",172,"Java");
        System.out.println(s instanceof Person);
        System.out.println(s instanceof Student);
    }
}
最后修改:2019 年 04 月 27 日 12 : 53 AM

发表评论