Java 继承、重写综合练习

final修饰类、修饰变量、修饰方法分别代表
final修饰变量:常量
final修饰方法:不可重写
final修饰类: 不能被继承

public class TestExtends {
    public static void main(String[] args) {
        Student stu1 = new Student("张三",180,"java");
        stu1.rest();
        stu1.study();
        System.out.println(stu1.name);

        //instanceof判断对象是否是这个类的实例对象
        System.out.println(stu1 instanceof Student);
        System.out.println(stu1 instanceof Person);

    }
}

class Person  {
    String name;
    int height;

    public void rest(){
        System.out.println("继承类学习");
    }
}

class Student extends  Person {
    String major;

    public void study(){
        System.out.println("Student.study");
    }

    public Student(String name,int height,String major){
        this.name = name;
        this.height = height;
        this.major = major;

    }


}

package com.itbaizhan.inherit;

/**
 * 测试重写
 */

public class TestOverride {
    public static void main(String[] args) {
        Horse h1 = new Horse();
        h1.run();

        Car c = new Car();
        c.stop();
    }
}
class Vehicle{

    //final 修饰方法,这个方法不能在子类中重写
    public final void addOil(){
        System.out.println("oillll!!!");

    }
public void run(){
    System.out.println("raning...");
}
public void stop(){
    System.out.println("stop!");
}
}
class Horse extends Vehicle{
    public void run(){
        System.out.println("策马奔腾!!!");
    }
}
class Car extends Vehicle {
    public void run() {
        System.out.println("飞驰人生!!!");
    }

    public void stop() {
        System.out.println("隧道中,禁止停车!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值