类的继承部分练习题

167 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

以下文章不是标准答案,是本人自己练习所写,仅供参考,如有错误请指正.考察到的知识点整理请参见我的关于基础知识点整合的文章

1.定义一个人的类(属性有名字,年龄。写一个能输出各个属性值的方法showInfo()),定义一个学生类(属性有性别),学生继承人类。

要求:(1)父类的属性赋值用构造方法来实现(分别用有参数构造方法和无参数构造方法实现)

(2)子类的属性也用构造方法来赋值。

(3)在子类中重写父类的showInfo()方法

(4)声明学生类的对象,调用学生的显示信息的方法。 ​

package work1;

public class Person {

    private String name;
    private Integer age;

    public void showInfo(){
        System.out.println("父类showInfo()方法  "+"姓名:"+name+",年龄:"+age);
    }

    public Person(){
        name="张三";
        age=21;
        System.out.println("父类无参构造方法  "+"姓名:"+name+",年龄:"+age);

    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("父类有参构造方法  "+"姓名:"+name+",年龄:"+age);

    }

    public static void main(String[] args) {

     //   Person person1=new Person();
     //   Person person2=new Person("李四",30);

    }
}

package work1;

public class Student extends Person{

    private String sex;

    public Student() {
        sex="女";
        System.out.println("子类无参构造方法  "+"性别:"+sex);
    }

    public Student(String sex) {
        super("李四",25);
        this.sex = sex;
        System.out.println("子类有参构造方法  "+"性别:"+sex);
    }

    public void showInfo() {
        System.out.println("子类重写父类的showInfo()方法");
    }

    public static void main(String[] args) {

        Student student1=new Student();
        Student student2=new Student("男");
        student1.showInfo();

    }

}

2.定义Animal父类,含有属性name,sex,age,方法sleep,run,编写一个子类Person,继承Animal类,然后调用父类的属性和方法 ​

\

package work2;

public class Animal {

    protected String name;
    protected String sex;
    protected Integer age;

    public void sleep(){
        System.out.println("父类的sleep方法");
    }

    public void run(){
        System.out.println("父类的run()方法");
    }
}

package work2;

public class Person extends Animal{

    public Person() {
        super.name="老虎";
        super.sex="母";
        super.age=1;
        System.out.println("子类调用父类的属性 "+"名字:"+name+",公母:"+sex+",年龄:"+age);
    }
    
    public void sleep() {
        super.sleep();
        System.out.println("子类调用父类Sleep()方法");
    }
    
    public void run() {
        super.run();
        System.out.println("子类调用父类的run()方法");
    }

    public static void main(String[] args) {
        Person person=new Person();
        person.sleep();
        person.run();
    }
}

3.试编写三个具有继承关系的类,A、B、C(B继承A,C继承B)。要求A中包含方法a1()、a2(),B类中的方法b1()访问了a1()和a2(),C类中的方法c1()访问了a1()、a2()、b1()。

**提示: **(a1(),a2(),b1(),c1()这些方法体除了实现题目要求之外,其余内容可以任意编写。) ​ ​

\

package work3;

public class A {

    public void a1(){
        System.out.println("A中的方法a1()");
    }

    public void a2(){
        System.out.println("A中的方法a2()");
    }
}

package work3;

public class B extends A{

    public void b1(){
        super.a1();
        super.a2();
        System.out.println("B中的方法b1()访问了a1()和a2()");
    }
}

package work3;

public class C extends B{

    public void c1(){
        super.a1();
        super.a2();
        super.b1();
        System.out.println("C中方法c1()访问了a1()、a2()、b1()");
    }

    public static void main(String[] args) {
        C c=new C();
        c.c1();
    }
}

4.请编码实现动物世界的继承关系:

动物(Animal)具有行为:吃(eat)、睡觉(sleep)

动物包括:兔子(Rabbit),老虎(Tiger)

这些动物吃的行为各不相同(兔子吃草,老虎吃肉);但睡觉的行为是一致的。

请通过继承实现以上需求,并编写测试类AnimalTest进行测试。

​​

\

package work4;

//将Animal定义为抽象类
public abstract class Animal {

    public abstract void sleep();

}

package work4;

//测试类
public class AnimalTest {

    private Rabbit rabbit=new Rabbit();
    private Tiger target=new Tiger();

    public void action(){
    rabbit.eat("草");
    rabbit.sleep();
    target.eat("肉");
    target.sleep();
    }

    public static void main(String[] args) {
        AnimalTest animalTest=new AnimalTest();
        animalTest.action();
    }
}

package work4;

public class Rabbit extends Animal{

    private String foot;

    public void eat(String foot){
        this.foot=foot;
        System.out.println("小兔子吃"+foot+"啦!");
    }

    public void sleep() {
        System.out.println("动物们都必须要睡觉!兔子也要");
    }
}

package work4;

public class Tiger extends Animal{

    private String foot;

    public void eat(String foot){
        this.foot=foot;
        System.out.println("老虎要吃"+foot+"!");
    }

    public void sleep() {
        System.out.println("动物们都必须要睡觉!老虎也要");
    }
}

 5.父类 Person 包含like()方法 子类 Son 重写父类方法并打印输出

package work5;

public class Person {

    public void like(){
        System.out.println("父类的like()方法");
    }
}

package work5;

public class Son extends Person{
    
    public void like() {
        System.out.println("子类重写的like()方法");
    }

    public static void main(String[] args) {
        Son son=new Son();
        son.like();
    }
}

​6.父类Employee   属性:name、sex , 带一个构造方法Employee(String n, char s),子类 Worker继承自Employee  属性:char category;//类别 boolean dressAllowance; //是否提供服装津贴 , 有一个构造方法 负责构造所有属性,还有一个自定义方法 isDressAll() 这个方法 负责通过判断dressAllowance的值输出 ,是否提供服装津贴。

新建一个类测试类InheDemo 。在main方法中新建一个Worker对象,输出这个对象的所有属性并调用isDressAll()方法得到津贴信息。

\

package work6;

public class Employee {

    private String name;
    private String sex;

    public Employee(String n, String s) {
        this.name = n;
        this.sex = s;
        System.out.println("姓名:"+n+",年龄"+sex);
    }
}

package work6;

public class InheDemo {

    public static void main(String[] args) {
        Employee employee=new Employee("张三","男");
        Worker worker=new Worker('是',true);
        worker.isDressAll();
    }
}

package work6;

public class Worker extends Exception{

    private char category;
    private boolean dressAllowance;

    public Worker(char category, boolean dressAllowance) {
        this.category = category;
        this.dressAllowance = dressAllowance;
        System.out.println("category:"+category+",dressAllowance:"+dressAllowance);
    }

    public boolean isDressAll(){
        boolean a=dressAllowance;
        System.out.println("是否津贴:"+a);
        return a;
    }
}

\