java继承练习题

一、选择

  1. 在Java中,以下程序的输出结果是
    A. Super–>print   B. Test–>print   C. Super–>print Test–>print   D. 编译错误
public class Super {
	public void print() {
		System.out.println("Supeer --> print");
	}
}
public class Test extends Super {
	public void print() {
		System.out.println("Test --> print");
	}
	public static void main(String[] args) {
		Super t = new Super();
		t.print();
	}
}

A

  1. 在Java中,以下关于方法重载和方法重写描述正确的是
    A. 方法重载和方法重写实现的功能相同
    B. 方法重载出现在父子关系中,方法重写是在同一类中
    C. 方法重载的返回类型必须一致,参数项必须不同
    D. 方法重写需要出现在满足继承关系的子类中

D

  1. 哪个选项中的方法插入到(1)处可以正确实现方法重写
    A. public static void bark(){ }
    B. public final void display(){ }
    C. public void eat(String food){ }
    D. public boolean eat(String food){ }
class Animal{
	public void eat(String food) {
	}
	public static void bark() {
	}
	public final void display() {
	}
}
class Cat extends Animal {
	//(1)
}

C

  1. 在下面程序的注释1处补充上下列()方法,会导致在编译过程中发生错误
    A. public float getNum() { return 4.0f; }
    B. private float getNum() {return 4.0f;}
    C. public void getNum(double d){ }
    D. public double getNum(float d){ return 4.0f; }

B

  1. 如下Java源文件,编译并运行Child.java后,以下结果描述正确的是
    A. 编译错误:没有找到构造器Child()
    B. 编译错误:没有找到构造器Parent1()
    C. 正确运行,没有输出值
    D. 正确运行,输出结果为:parent2
public class Parent1 {
	Super(String s) {
		System.out.println(s);
	}
}
public class Test extends Parent2 {
	Parent2() {
		System.out.println("parent2");
	}
}
public class Child extends Parent2 {
	public static void main(String[] args) {
		Child child = new Child();
	}
}

B

  1. 分析如下所示的Java代码,则选项中的说法正确的是
    A. 第2行错误,Test类的构造函数中参数名称应与其父类构造函数中的参数名相同
    B. 第3行错误,应使用super关键字调用父类的name属性,改为super.name=“hello”
    C. 第4行错误,调用父类构造方法的语句必须放在子类构造方法中的第一行
    D. 程序编译通过,无错误
public class Parent {
	public String name;
	Super(String pName) {
		this.name = pName;
	}
}
public class Test extends Parent {
	public Test(String Name) { //2
		name = "hello"; //3
		super("kittv"); //4
	}
}

C

  1. 关于super的说法正确的是
    A. 是指当前子类的对象   B. 是指当前类的对象   C. 是指当前父类的对象   D. 可以用在main()方法中

C

  1. 阅读下面JAVA代码片段,正确的选项是
    A. 第1行编译错误,但能输出正确结果
    B. 第2行编译错误,但能输出正确结果
    C. 第3行编译错误,不能输出正确结果
    D. 第4行编译错误,不能输出正确结果
public class Car {
	String color;
	String motor;
	public Car(String color, String motor) { //1
		this.color = color;
		this.motor = motor;
	}
}
public class Truck extends Car {
	double weight;
	public Truck() {  //2
		
	}
	public Truck(String color, String motor, double weight) { //3
		super(color,motor); 
		this.weight = weight; 
		
	}
	public void display() { 
		System.out.println("颜色:" + color + "\t发动机的型号:" + motor + "\t载重量:" + weight );
		
	}
	public static void main(String[] args) {
		Truck truck = new Truck("红色","玉柴",1.5); //4
		truck.display();
	}
}

B

  1. 下列关于super和this的说法正确的是(多选)
    A. this关键字通常指当前对象
    B. super关键字则指父类对象
    C. 在一个类中this只可以调用当前类中公有属性和方法
    D. 在一个类中super可以调用父类中允许被访问的属性和方法

A B D

  1. 下列关于Object类的叙述错误的是
    A. Object类是所有类的父类
    B. 所有类都可以继承Object中允许被继承的方法
    C. 一个类没有使用extends关键字明确标识继承关系,则默认继承Object类
    D. 要想继承Object类,必须使用extends关键字标识继承关系,否则不会实现继承

D

  1. 该段代码的运行结果为
    A. true   B. 相等   C. 不相等   D. false
public class Father {
	private String name;
	private int age;
	public Father(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public static void main(String[] args) {
		Father fa1 = new Father("晓明",13);
		Father fa2 = new Father("晓明",13);
		boolean flag = fa1.equals(fa2);
		System.out.println(flag);

D

  1. 在Java中,关于继承的说法错误的是
    A. 使用extends关键字实现一个类继承另一个类 B. 所有的Java类都直接或间接地继承了java.lang.Object类 C. 在子类的构造方法中,必须显式调用父类的构造方法 D. 在子类的构造方法中,可以通过super关键字调用父类的构造方法

C

二、编程

  1. 编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行 车、电动车和三轮车。
    1. 第一步:分析自行车、电动车和三轮车的共性:
      都是非机动车,具有非机动车的基本特征 2. 都有运行的方法
    2. 第二步:根据共性,定义非机动车
      属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
      方法:
      1. 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和 颜色的赋值;在四参构造方法中,完成对所有属性的赋值
      2. 编写运行的方法,描述内容为:这是一辆 * 颜色的,* 牌的非机动车,有 * 个轮子,有 * 个座椅 的非机动车。其中 * 的数据由属性提供
    3. 第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:
      • 自行车类:
        1. 在构造方法中调用父类多参构造,完成属性赋值
        2. 重写运行方法,描述内容为:这是一辆 * 颜色的, * 牌的自行车。其中 * 的数据由属性提供
      • 电动车类:
        1. 增加“电池品牌”属性
        2. 重写运行方法,描述内容为:这是一辆使用 * 牌电池的电动车。其中 * 的数据由属性提供
      • 三轮车类:
        1. 在无参构造中实现对轮子属性值进行修改
        2. 重写运行方法,描述内容为:三轮车是一款有 * 个轮子的非机动车。其中 * 的数据由属性提供
/**
* 父类:非机动车
**/
public class Vehicle {
	//定义父类非机动车属性
	private String logo;
	private String color;
	private int wheel;
	private int seat;
	//get/set方法
	public String getLogo() {
		return logo;
	}
	public void setLogo(String logo) {
		this.logo = logo;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public int getWheel() {
		return wheel;
	}
	public void setWheel(int wheel) {
		this.wheel = wheel;
	}
	public int getSeat() {
		return seat;
	}
	public void setSeat(int seat) {
		this.seat = seat;
	}
	//定义无参构造
	public Vehicle() {
		super();
		this.setWheel(2);
		this.setSeat(1);
	}
	//定义双参构造
	public Vehicle(String logo, String color) {
		super();
		this.setLogo(logo);
		this.setColor(color);
	}
	//定义四参构造
	public Vehicle(String logo, String color, int wheel, int seat) {
		super();
		this.setLogo(logo);
		this.setColor(color);
		this.setWheel(wheel);
		this.setSeat(seat);
	}
	//运行方法
	public String info() {
		String str = "这是一辆" + this.getColor() + "颜色的,";
		str += this.getLogo() + "牌的非机动车,有" + this.getWheel() + "个轮子,";
		str += "有" + this.getSeat() + "个座椅";
		return str;
	}
}
/**
* 自行车类
**/
public class Bicycle extends Vehicle {
	public Bicycle() {
		super();
	}
	public Bicycle(String logo, String color) {
		super(logo, color);
	}
	//重写运行方法
	public String info() {
		String str = "这是一辆" + this.getColor() + "颜色的,";
		str += this.getLogo() + "牌的自行车";
		return str;
	}
}
/**
* 电动车类
**/
public class ElecBicycle extends Vehicle {
	//新增电池属性
	private String eleclogo;
	//set/get方法
	public String getEleclogo() {
		return eleclogo;
	}
	public void setEleclogo(String eleclogo) {
		this.eleclogo = eleclogo;
	}
	//无参构造
	public ElecBicycle() {
		super();
	}
	//有参构造
	public ElecBicycle(String eleclogo) {
		this.setEleclogo(eleclogo);
	}
	//重写运行方法
	public String info() {
		String str = "这是一辆使用" + this.getEleclogo() + "牌电池的电动车";
		return str;
	}
}
/**
* 三轮车类
**/
public class Tricycle extends Vehicle {
	//无参构造
	public Tricycle() {
		super();
		this.setWheel(3);
	}
	//重写运行方法
	public String info() {
		String str = "三轮车是一款有" + this.getWheel() + "个轮子的非机动车";
		return str;
	}
}
/**
* 测试类
**/
public class Test {
	public static void main(String[] args) {
		Vehicle v = new Vehicle("天宇","红",4,2);
		System.out.println("父类信息测试" + v.info());
		Bicycle b = new Bicycle("捷安特","黄");
		System.out.println("自行车类信息测试" + b.info());
		ElecBicycle e = new ElecBicycle("飞鸽");
		System.out.println("电动车类信息测试" + e.info());
		Tricycle t = new Tricycle();
		System.out.println("三轮车类信息测试" + t.info());
	}
}
  1. 请使用面向对象的思想,设计自定义类Person继承Object类,重写toString方法实现对象信息输出。
    • 创建一个 Person 类继承自 Object,其中类的结构要求为: 属性:name(姓名)、age(年龄)、sex(性别)
    • 方法: 创建带参(name、age、sex为参数)构造方法 重写 toString 方法,输出信息格式为:姓名:* 年龄: * 性别: * (其中, * 为对象对应属性值)
    • 创建测试类,在测试方法中,实例化 Person对 象,并传入三个属性值。然后,分别通过直接 打印Person对象以及利用重写的 toString 方法,打印输出2行对象信息。
/**
* Person类
**/
public class Person {
	//私有属性:name(姓名)、age(年龄)、sex(性别)   
	private String name;
	private int age;
	private String sex;
	//带参构造方法(name、age、sex为参数)     
	public Person(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	//无参构造方法
	public Person() {
		super();
	}
	//通过封装实现对属性的get/set方法设定 
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	//重写toString方法,表示形式为:姓名:+**+  年龄:+**+  性别:+**
	public String toString() {
		String str = "姓名:" + this.getName() + "   ";
		str += "年龄:" + this.getAge() + "   ";
		str += "性别:" + this.getSex();
		return str;
	}
}
/**
* 测试类
**/
public class Test {
	public static void main(String[] args) {
		Person p = new Person("李明",18,"男");
		System.out.println(p);
		System.out.println(p.toString());
	}
}
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一道 Java 继承练习题: 假设有一个父类 `Animal` 和两个子类 `Dog` 和 `Cat`。`Animal` 类包含一个 `name` 成员变量和一个 `eat` 方法,`Dog` 和 `Cat` 类都继承了 `Animal` 类,并且都有一个 `sound` 方法。现在请你完成以下练习: 1. 在 `Animal` 类中,添加一个 `sleep` 方法,并在 `Dog` 和 `Cat` 类中覆盖(重写)该方法,使得 `Dog` 和 `Cat` 的 `sleep` 方法输出不同的内容。 2. 在 `Dog` 和 `Cat` 类中,实现各自的 `sound` 方法,使得 `Dog` 的 `sound` 方法输出 `"汪汪"`,`Cat` 的 `sound` 方法输出 `"喵喵"`。 3. 编写一个名为 `Main` 的测试类,并在其中创建一个 `Dog` 对象和一个 `Cat` 对象,分别调用它们的 `eat`、`sleep` 和 `sound` 方法。 你可以参考以下代码: ```java public class Animal { private String name; public Animal(String name) { this.name = name; } public void eat() { System.out.println(name + " is eating"); } public void sleep() { System.out.println(name + " is sleeping"); } } public class Dog extends Animal { public Dog(String name) { super(name); } public void sleep() { System.out.println(getName() + " is sleeping soundly"); } public void sound() { System.out.println("汪汪"); } } public class Cat extends Animal { public Cat(String name) { super(name); } public void sleep() { System.out.println(getName() + " is sleeping quietly"); } public void sound() { System.out.println("喵喵"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog("旺财"); Animal cat = new Cat("小花"); dog.eat(); dog.sleep(); ((Dog) dog).sound(); cat.eat(); cat.sleep(); ((Cat) cat).sound(); } } ``` 希望这个练习题能够帮助你理解 Java 继承的相关知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值