栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

extends继承与insatnceof实例运算符

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

extends继承与insatnceof实例运算符

public class person {
    int id;
    String mane;
    public void rest(){
        System.out.println("休息");

    }

    public static void main(String[] args) {
        Student s=new Student(2001, "张三",80);

        System.out.println(s instanceof person);
        System.out.print(s instanceof Student);
    }
    static class Student extends person {

        int score;
        public void study(){
            System.out.println("学习");
        }
        Student(int id,String mane,int score ){
            this.id=id;
            this.mane=mane;

            System.out.println(id+"+"+mane+"+"+score);

        }

}
}

 类中包含:属性,方法,构造器。

在继承关系中:除了构造器以外,其他的属性,方法都可以引用。

public class person {


}

 public访问修饰的class person类称之为:父类

static class Student extends person {



}

用static修饰的class Studnet类为:子类

java中类的继承:在java中类启用单继承类似于一棵粗大的树由一个主干衍生出许多的分支。(可以类比于社会中的关系

父亲

Java.lang.object

public class person
你儿子static class Student

引用实例如下:

   static class Student extends person {

        int score;
        public  static void study(){
            System.out.println("学习");
        }
        Student(int id,String mane,int score ){
            this.id=id;
            this.mane=mane;

子类Student用extends调用person父类:引用了父类中的属性,所以可以直接在构造器中初始化属性   id   mane。

instanceof二元运算符:

左边为对象右变为类,返回值的类型为true或false。

 public static void main(String[] args) {
        Student s=new Student(2001, "张三",80);
        rest();
        System.out.println(s instanceof person);
        System.out.print(s instanceof Student);

在代码中s为 对象      可以调用Student,又因为person为Student的父类,所以可以调用person

我是小土狗,欢迎指正

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号