java练习题之继承

发布时间:2023年12月22日

1:(继承)关于继承描述错误的是(B)

A. 继承体现的是类与类之间的"is-a"关系

B. 通过继承,子类可以直接访问父类中所有的属性和方法????私有的不可以访问

C. Java 中的继承是单继承的关系

D. 父类是子类共性的提取

2:(修饰符)下列关于访问修饰符访问权限描述错误的是(C)

  1. private :只能在本类中使用
  2. default :本类+同包
  3. protected :本类+同包+不同包 ???同包子类 ?不同包子类
  4. public: 本类+同包+不同包

3: (覆盖)下列关于方法覆盖描述错误的是(BD)

A. 子类中的方法名必须和父类中方法名相同

B. 子类中的方法参数列表和父类的不同??参数列表必须相同

C. 子类中的方法返回值类型和父类相同

D. 父类的方法访问修饰符和子类中的相同或是更宽????子类的相同或更宽

4:(继承)关于 Java 中的继承,以下说法正确的是(C)

A. 一个子类可以有多个直接的父类,一个父类也可以有多个直接的子类????子类只能有一个直接父类

B. 一个子类可以有多个直接的父类,但是一个父类只可以有一个直接的子类

C. 一个子类只能有一个直接的父类,但是一个父类可以有多个直接的子类

D. 以上说法都不对

5:(访问修饰符)仔细阅读以下代码,描述正确的是(C)

A. 编译通过

B. 编译不通过,应把第 11?行改成 super.valu?e = value;

C. 编译不通过,应把第 11?行改成 super(value);

D. 编译不通过,可以为 MySubClass 增加一个 value 属性

E. 编译不通过,把第 4 行改为 protected int value; 把第 11?行改为 super.value = value;

6:(重写)仔细阅读以下代码,写出代码执行的结果:

Super()

Sub()

Super()

Sub()

Sub(int)

Super(String)

Sub(String)

7:(super)仔细阅读以下代码,写出代码执行的结果。

m1() int sub

m1() int super

m2() int super

8:(super)仔细阅读以下程序,请问如何修改代码才能通过?

第二行加一个 ??String ?str;

9:(访问修饰符)仔细阅读以下代码,以下代码有哪些地方编译出错?假设不允许修改 MyClass 类,

那应该如何修改?

第一个System.out.println(mc1.getValue);

System.out.println(mc2.getValue);

第二个 改包名为:package corejava.Chp7;或者删除 MyClass mc2=new MyClass(10);

System.out.println(mc1.getValue);

System.out.println(mc2.getValue);

10:(覆盖)阅读以下代码,哪些代码写在//1 处,程序编译能通过(A)

  1. public int method(){return 0;}
  2. void method(){}
  3. void method(int n){}
  4. protected void method(){}

11:(对象创建过程)仔细阅读以下代码,写出代码的执行结果:

Meal()

Lunch()

Vategable()

Potato()

Meat()

Vategable()

Tomato();

Sandwish()

12:(重写)阅读以下代码,哪些代码写在//1 处,程序编译能通过(BD)

  1. public int method(){return 0;}
  2. void method(){}
  3. void method(int n){}
  4. private void method(){}

13:(继承+封装)编程:定义一个人类(Person),包括属性:姓名、性别、年龄、国籍;

包括的方法:吃饭、睡觉,工作

(1) 根据人类,定义一个学生类,增加属性:学校、学号;重写工作方法(实现内容为学习)

(2) 根据人类,定义一个工人类,增加属性:单位,工龄;重写工作方法

(3) 根据学生类,定义一个学生干部类(StudentLeader),增加属性:职务;增加方法:开会

(4) 定义一个测试类,分别创建上述 3 类具体人物的对象并将信息打印在控制台上。

package com.by.homework3;

public class Person {
????String name;
????String sex;
????int age;
????String nationality;
????public void eat(){
????????System.out.println("人的吃饭方法");
????}
????public void sleep(){
????????System.out.println("人的睡觉方法");
????}
????public void work(){
????????System.out.println("人的工作方法");
????}
}



package com.by.homework3;

public class Student extends Person{
????String school;
????String schoolNum;

????public Student() {
????}
????public void work(){
????????System.out.println("学生的学习方法");
????}
}



package com.by.homework3;

public class Worker extends Person{
????String depart;
????String seniority;
????public void work(){
????????System.out.println("工人的工作方法");
????}
}



package com.by.homework3;

public class StudentLead?extends Student?{
????String zhiwu;
????public void kaihui(){
????????System.out.println("学生的开会方法");
????}
}



Student student=new Student();
student.name="大黄";
student.age=19;
student.sex="男";
student.nationality="中国";
student.school="一中";
student.schoolNum="123";
System.out.println(student.name);
System.out.println(student.age);
System.out.println(student.sex);
System.out.println(student.nationality);
System.out.println(student.school);
System.out.println(student.schoolNum);
student.work();

Worker worker=new Worker();
worker.name="haha";
worker.age=18;
worker.sex="nan";
worker.nationality="CN";
worker.depart="show";
worker.seniority="6";
System.out.println(worker.name);
System.out.println(worker.age);
System.out.println(worker.sex);
System.out.println(worker.nationality);
System.out.println(worker.depart);
System.out.println(worker.seniority);
worker.work();



StudentLead studentLead=new StudentLead();
studentLead.name="";
studentLead.age=;
studentLead.sex="";
studentLead.nationality="";
studentLead.school="";
studentLead.schoolNum="";
studentLead.zhiwu="";
System.out.println(studentLead.name);
System.out.println(studentLead.age);
System.out.println(studentLead.sex);
System.out.println(studentLead.nationality);
System.out.println(studentLead.school);
System.out.println(studentLead.schoolNum);
System.out.println(studentLead.zhiwu);
studentLead.kaihui();

14:(继承+封装)编程:定义一个交通工具类(Vehicles),该类的属性为:商标(brand)、颜色(color);

功能方法为:run 方法(行驶功能,控制台输出“车已经启动”)、showInfo(显示信息,控制台输出商标和颜色)

(1) 编写一个小汽车类(Car)继承于 Vehicles 类,添加属性座位(seats);成员方法 showCar(显示小汽车的所有信息)

(2) 编写一个卡车类(Truck)继承于 Vehicles 类,添加属性载重(load);成员方法 showTruck(显示卡车的所有信息)

(3) 定义测试类,分别创建 Car 对象和 Truck 对象,控制台打印输出的信息如下:

商标:奔驰,颜色:白色,座位:5

商标:福田,颜色:红色,载重:6.5 吨

package com.by.homework3;

public class Vehicles {
????String brand;
????String color;
????public void run(){
????????System.out.println("车子已经启动");
????}
????public void showInfo(){
????????System.out.println(brand);
????????System.out.println(color);
????}

}





package com.by.homework3;

public class Car extends Vehicles{
????String seats;
????public void showCar(){
????????showInfo();
????????System.out.println(seats);
????}
}



package com.by.homework3;

public class Truck extends Vehicles {
????String load;
????public void showTruck(){
????????showInfo();
????????System.out.println(load);
????}
}



package com.by.homework3;

import com.by.Obj.Teacher;

public class TestCar {
????public static void main(String[]args){
????????Car car=new Car();
????????car.brand="奔驰";
????????car.color="白色";
????????car.seats="5";
????????System.out.println(car.brand+car.color+car.seats);
????????Truck truck=new Truck();
????????truck.brand="福田";
????????truck.color="红色";
????????truck.load="6.5吨";
????????System.out.println(truck.brand+truck.color+truck.load);
????}
}

15:编程:有以下几个类,根据下面的继承关系,用 Java 代码实现:

(1) Circle 类(圆形),属性:半径;方法:求周长、求面积

(2) Rect 类(矩形),属性:长、宽;方法:求周长、求面积

(3) Square 类(正方形),属性:边长;方法:求周长、求面积

提示: ① 这三个类均具有求周长和面积的方法 ② 正方形是特殊的矩形

package com.by.homework3.figure;

public class Figure {
????public void qiuzhouchang(){
????????System.out.println("周长");
????}
????public void qiumianji(){
????????System.out.println("面积");
????}
}



package com.by.homework3.figure;

public class Square extends Figure{
????int bianchang;

????public Square(){

????}
????public Square(int bianchang) {
????????this.bianchang = bianchang;
????}

????public void qiuzhouchang(){
????????int zhouchang=bianchang*4;
????????System.out.println("正方形的周长:"+zhouchang);
????}
????public void qiumianji(){
????????int mianji=bianchang*bianchang;
????????System.out.println("正方形的面积:"+mianji);
????}
}





package com.by.homework3.figure;

import com.by.homework3.figure.Figure;

public class Rect extends Figure {
?????int chang;
?????int kuan;

????public Rect() {
????}

????public Rect(int chang,int kuan) {
????????this.chang = chang;
????????this.kuan=kuan;
????}

????@Override
????public void qiuzhouchang() {
????????int zhouchang=chang*2+kuan*2;
????????System.out.println("矩形的周长为:"+zhouchang);
????}

????@Override
????public void qiumianji() {
????????int mianji=chang*kuan;
????????System.out.println("矩形面积为:"+mianji);
????}
}



package com.by.homework3.figure;

public class Circle extends Figure{
????int banjin;

????@Override
????public void qiuzhouchang() {
????????Double zhouchang=2*3.14*banjin;
????????System.out.println("圆的周长:"+zhouchang);
????}

????@Override
????public void qiumianji() {
????????Double mianji=3.14*banjin*banjin;
????????System.out.println("圆的面积:"+mianji);
????}
}



package com.by.homework3.figure;

public class Test {
????public static void main(String[]args){
????????Square square=new Square(10);
????????square.qiuzhouchang();
????????square.qiumianji();

????????Rect rect=new Rect(15,10);
????????rect.qiuzhouchang();
????????rect.qiumianji();
????????
????????Circle circle=new Circle(5);
????????circle.qiuzhouchang();
????????circle.qiumianji();
????}
}

16:编程:某公司的雇员分为以下若干类:

(1) Employee:这是所有员工总的父类。

① 属性:员工的姓名,员工的生日月份

② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励 100 元。

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。

① 属性:月薪。

(3) HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出 160 小时的部分按照 1.5 倍工资发放。

① 属性:每小时的工资、每月工作的小时数。

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。

① 属性:月销售额、提成率。

(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。

① 属性:底薪。

要求:

(1) 创建 SalariedEmployee、HourlyEmployee、SaleEmployee、BasePlusSalesEmployee四个类的对象各一个

(2) 分别调用getSalary(int money)方法计算某个月这四个对象各自的工资

注意:要求把每个类都做成完全封装,不允许非私有化属性。

package com.by.homework3.employee;

public class Employee {
????private String name;
????private int brith;

????public Employee(String name, int brith) {
????????this.name = name;
????????this.brith = brith;
????}
}



package com.by.homework3.employee;

public class HourlyEmployee extends Employee {
????private int hourSalary;
????private double hours;

????public double getHours() {
????????return hours;
????}

????public void setHours(double hours) {
????????this.hours = hours;
????}

????public HourlyEmployee(String name, int brith, int hourSalary, double hours) {
????????super(name, brith);
????????this.hourSalary = hourSalary;
????????this.hours = hours;
????}
}



package com.by.homework3.employee;

import javax.xml.namespace.QName;

public class SalariedEmployee extends Employee{
????private int monthSalary;

????public SalariedEmployee(String name, int brith, int monthSalary) {
????????super(name, brith);
????????this.monthSalary = monthSalary;
????}
????public double getMonth() {
????????return monthSalary;
????}
????public void setMonth(int monthSalary) {
????????this.monthSalary = monthSalary;
????}
????public double getSalary(int month){
????????return monthSalary + this.getSalary(month);
????}
}



package com.by.homework3.employee;

public class SalesEmployee extends Employee {
????private int monthlySales;
????private double royaltyRate;

????public SalesEmployee(String name, int brith, int monthlySales, double royaltyRate) {
????????super(name, brith);
????????this.monthlySales = monthlySales;
????????this.royaltyRate = royaltyRate;
????}
}



package com.by.homework3.employee;

public class BasePlusSaleEmployee extends SalesEmployee{
????private int diXin;

????public BasePlusSaleEmployee(String name, int brith, int monthlySales, double royaltyRate) {
????????super(name, brith, monthlySales, royaltyRate);
????}

????public int getDiXin() {
????????return diXin;
????}
????public void setDiXin(int diXin) {
????????this.diXin = diXin;

// ?????????????
????????????this.diXin = diXin;
????????}
????}

17:编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。

提示:

(1)编写一个圆类Circle,该类拥有:

1)一个成员变量,radius(私有,浮点型);//存放圆的半径;

2)两个构造方法

Circle() //将半径设为0

Circle(double r ) //创建Circle对象时将半径初始化为r

3)三个成员方法

double getArea() //获取圆的面积

double getPerimeter() //获取圆的周长

void show() //将圆的关径、周长、面积输出到屏幕

(2) 编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:

1)一个成员变量,double hight (私有,浮点型); //圆柱体的高;

2)构造方法

//创建Cylinder对象时将半径初始化为r,高度初始化为h

Cylinder(double r,double h)

3)成员方法

double getVolume() //获取圆柱体的体积

void showVolume() //将圆柱体的体积输出到屏幕

package com.by.homework3.circle17;

public class Circle {
????private double radius;

????public Circle() {
????}

????public double getRadius() {
????????return radius;
????}

????public Circle(double radius) {
????????this.radius = radius;
????}
}



package com.by.homework3.circle17;

public class Cylinder extends Circle {
????private double hight;

????public Cylinder() {
????}

????public Cylinder(double hight) {
????????this.hight = hight;
????}
}

文章来源:https://blog.csdn.net/zhzjn/article/details/135140299
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。