java-继承和抽象类练习

目录

前言

      这是我从网上看到的题目,代码我自己打出来的,只注重了功能,所以写得不好的地方,请见谅。本次的题目有点怪,就看它的步骤来的。

题目1

1.基础概念概念
(1) 什么叫做类与类的继承,作用是什么?

  • 继承就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性、相同的行为。
  • 作用就是子类复用父类的内容
    (2) 继承后,父类与子类之间,各成员有什么样的影响?
  • 成员变量
    * 不重名,没有影响
    * 重名,就近使用,使用super区分分类的变量
  • 构造方法
    * 无影响,但是子类的构造方法默认调用父类的构造方法
  • 成员方法
    * 不重名,没有影响
    * 重名,子类重写父类方法
    (3) 子类中,如何调用父类的成员?如何使用本类的成员?
  • 父类成员方法:super.方法名
  • 父类非私有成员变量:super.变量名
  • 子类成员方法:this.方法名
  • 子类成员变量:this.变量名
    (4)抽象方法与普通成员方法有什么区别?
  • 抽象方法使用abstract关键字修饰,没有方法体
  • 成员方法有方法体
    (5)抽象类与普通类有什么区别
  • 方法
    * 抽象类可以包含抽象方法和成员方法
    * 普通类不可以包含抽象方法,只有成员方法
  • 对象
    * 抽象类不可以创建对象
    * 普通类可以创建对象

题目2

  1. 按步骤编写代码,效果如图所示:
    在这里插入图片描述
  • 编写步骤:

    1. 定义抽象类A,抽象类B继承A,普通类C继承B
    2. A类中,定义成员变量numa,赋值为10,抽象showA方法。
    3. B类中,定义成员变量numb,赋值为20,抽象showB方法。
    4. C类中,定义成员变量numc,赋值为30,重写showA方法,打印numa,重写showB方法,打印numb,定义showC方法,打印numc。
      测试类中,创建C对象,调用showA方法,showB方法,showC方法。
public abstract class A {
    int numa = 10;
    abstract void showA();
}


public abstract class B extends A{
    int numb = 20;
    abstract void showB();
}

public class C  extends  B{
    int numc = 30;
    @Override
    void showB() {
        System.out.println(super.numb);
    }

    @Override
    void showA(){
        System.out.println(super.numa);
    }

    void showC(){
        System.out.println(numc);
    }
}

public class Test02 {
    public static void main(String[] args) {
        C c = new C();
        c.showA();
        c.showB();
        c.showC();
    }
}

题目3

  1. 按步骤编写代码,效果如图所示:
    在这里插入图片描述
  • 编写步骤:

    1. 模拟农学院动物医疗系统信息。
    2. 定义抽象家禽类(Poultry)
    3. 私有成员变量:动物种类(name),症状(symptom),年龄(age), 病因(illness)
    4. 提供空参和带参构造方法
    5. 成员方法:
      1. 抽象方法症状(showSymptom)
      2. 普通方法基本信息(showMsg)
      3. 提供setXxx和getXxx方法
    6. 定义普通鸭子类(Duck)
      1. 提供空参和带参构造方法
      2. 重写showSymptom方法,打印症状信息。

代码如下:

public abstract class Poultry {
    private String name;
    private String symptom;
    private int age;
    private String illness;

    public Poultry() {
    }

    public Poultry(String name, String symptom, int age, String illness) {
        this.name = name;
        this.symptom = symptom;
        this.age = age;
        this.illness = illness;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSymptom() {
        return symptom;
    }

    public void setSymptom(String symptom) {
        this.symptom = symptom;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getIllness() {
        return illness;
    }

    public void setIllness(String illness) {
        this.illness = illness;
    }

    abstract void showSymptom();
    public void showMsg(){
        System.out.println(name+" "+age+" "+symptom+" "+ illness);
    }
}

public class Duck extends Poultry{
    public Duck() {
    }

    public Duck(String name, String symptom, int age, String illness) {
        super(name, symptom, age, illness);
    }

    @Override
    void showSymptom() {
        System.out.println(getSymptom());
    }
}
public class test03 {
    public static void main(String[] args) {
        Duck duck = new Duck("鸭子", "感冒", 2, "发烧");
        duck.showMsg();
        duck.showSymptom();
    }
}

题目4

  1. 按步骤编写代码,效果如图所示:
    在这里插入图片描述
  • 编写步骤:

    1. 模拟教学管理系统师生信息。
    2. 定义Person类。
      1. 属性:姓名、年龄
      2. 构造方法:无参构造方法,有参构造方法
      3. 成员方法:getXxx方法,setXxx方法,显示基本信息showMsg方法
    3. 定义Teacher类,继承Person
      1. 属性:学科
      2. 构造方法:无参构造方法,有参构造方法
      3. 成员方法:getXxx方法,setXxx方法,讲课方法
    4. 定义Student类,继承Person
      1. 属性:分数
      2. 构造方法:无参构造方法,有参构造方法
      3. 成员方法:getXxx方法,setXxx方法,考试方法

代码如下:

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void showMsg(){
        System.out.println(name+ " " + age);
    }
}
public class Teacher extends Person{
    private String objectoftech;

    public Teacher() {
    }

    public Teacher(String name, int age, String objectoftech) {
        super(name, age);
        this.objectoftech = objectoftech;
    }

    public String getObjectoftech() {
        return objectoftech;
    }

    public void setObjectoftech(String objectoftech) {
        this.objectoftech = objectoftech;
    }

    public void tech(){
        System.out.println(getName()+"老师"+",讲授"+objectoftech+"课");
    }
}
public class Student extends Person{
    private int score;

    public Student(){

    }

    public Student(String name, int age, int score) {
        super(name, age);
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public void test(){
        System.out.println(getName()+"同学,考试得了:"+getScore()+"分");
    }
}
public class Test04 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("王小平",33,"java");
        teacher.tech();

        Student student = new Student("李小乐",14,90);
        student.test();
    }
}

题目5

  1. 按步骤编写代码,效果如图所示:
    在这里插入图片描述
  • 编写步骤

    1. 模拟汽车网站信息。
    2. 定义汽车Auto类
      1. 属性:品牌,车长,价格
    3. 定义SUV继承Auto类
      1. 属性:小型车车长标准值:4295,中型车车长标准值:5070。
      2. 定义判断车型方法
        1. 判断小型车:小于小型车车长标准值
        2. 判断大型车:大于中型车车长标准值
        3. 判断中型车:大于小型车车长标准值并且小于等于中型车车长标准值
    4. 测试类中,创建若干SUV对象,保存到集合,遍历集合,输出中型SUV。

代码如下:

public class Auto {
    private String brand;
    private int length;
    private int price;

    public Auto() {
    }

    public Auto(String brand, int length, int price) {
        this.brand = brand;
        this.length = length;
        this.price = price;
    }


    public Auto(int length, int price) {
        this.length = length;
        this.price = price;
    }
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

public class SUV extends Auto{
    private int lengthstandard =4295;
    private int mindstandard = 5070;

    public SUV() {
    }

    public SUV(String brand, int length, int price) {
        super(brand, length, price);
    }
    public SUV( int length, int price) {
        super(length, price);
    }
    public boolean checksmall(){
        if(getLength() < lengthstandard){
            System.out.println("小型车");
            return true;
        }
        return false;
    }

    public boolean checkmid(){
        super.setBrand("SUV");
        if(getLength() > lengthstandard && getLength()<= mindstandard){
            System.out.println("中型车");
            return true;
        }
        return false;
    }

    public boolean checkbig(){
        if(getLength() > mindstandard){
            System.out.println("大型车");
            return true;
        }
        return false;
    }
    public void showMes(){
        System.out.println(getBrand()+" "+getLength()+" "+getPrice());
    }
}
public class Test05 {
    public static void main(String[] args) {
        // 创建SUV对象
        SUV suv1 = new SUV(5079, 750000);
        SUV suv2 = new SUV(4813, 760000);
        SUV suv3 = new SUV(4270, 127800);
        SUV suv4 = new SUV(4545, 188800);

        ArrayList<SUV> arrayList = new ArrayList<>();
        arrayList.add(suv1);
        arrayList.add(suv2);
        arrayList.add(suv3);
        arrayList.add(suv4);


        for (SUV suv:arrayList
             ) {
            if(suv.checkmid()){
                suv.showMes();
            }

        }
    }
}

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值