学习笔记06-第七章 继承
用书参考:孙连英,刘畅,彭涛所著的Java面向对象程序设计。
(1)设计Vehicle类,包含的属性有注册码,制造商,生产年份和价格
题目简介
设计Vehicle类,包含的属性有注册码,制造商,生产年份和价格
要求:前三个属性在创建时设置,价格允许改变。能够读取上述所有属性。
重写tostring方法,使输出格式为:
注册码: ,车辆制造商:,生产年份:,
价格是:元。
提供一个方法,接收一个年月日作为输入,返回车辆的年龄。
设计vechile类的子类SecondHandVehicle。
子类具有额外的属性numberOfOwner,该属性在创建时被设置,并具有读取操作。提供一个
方法返回二手车的交易次数。
编写测试类,测试上述类的所有方法。
代码
import java.util.Scanner; public class Text7_1 {
public static void main(String[] args) {
/*设计Vehicle类,包含的属性有注册码,制造商,生产年份和价格 * 要求:前三个属性在创建时设置,价格允许改变。能够读取上述所有属性。 * 重写tostring方法,使输出格式为: * 注册码: *,车辆制造商:*,生产年份:, * 价格是:元。 * 提供一个方法,接收一个年月日作为输入,返回车辆的年龄。 * 设计vechile类的子类SecondHandVehicle。 * 子类具有额外的属性numberOfOwner,该属性在创建时被设置,并具有读取操作。提供一个 * 方法返回二手车的交易次数。 * 编写测试类,测试上述类的所有方法。 * */ testVehicle test1 = new testVehicle(); test1.test(); } } //测试类的编写 class testVehicle {
public void test() {
SecondHandVehicle car = new SecondHandVehicle("A1", "比亚迪", 2019, 1); car.setM_price(); System.out.println("车辆基本信息"+car.toString()); System.out.println(car.getM_registerCode()+"的价格为:"+car.getM_price()); System.out.println("制造商为:"+car.getM_maker()); System.out.println("这款车的交易次数为:"+car.calNum()); int num = car.calu(); System.out.println("这款车的车龄为:"+num); } } //子类二手车 class SecondHandVehicle extends Vehicle {
int numberOfOwner; //调用父类的构造方法 public SecondHandVehicle(String registerCode, String maker, int year,int numberOfOwner) {
super(registerCode, maker, year); this.numberOfOwner = numberOfOwner; } public int calNum() {
return this.numberOfOwner; } } //设计父类Vehicle class Vehicle {
private String m_registerCode; private String m_maker; private int m_year; private double m_price = 0; //构造方法 public Vehicle(String registerCode,String maker,int year) {
this.m_registerCode = registerCode; this.m_maker = maker; this.m_year = year; } public double getM_price() {
if(this.m_price ==0) {
System.out.println("未设置价格"); return 0; } else {
return m_price; } } public void setM_price(double m_price) {
this.m_price = m_price; } public String getM_registerCode() {
return m_registerCode; } public String getM_maker() {
return m_maker; } public int getM_year() {
return m_year; } //重写tostring public String toString() {
return "注册码是:"+this.m_registerCode+","+"车辆制造商:"+this.m_maker+","+"生产年份:"+this.m_year+","+ "价格是:"+this.m_price+"元。"; } public int calu() {
Scanner sc = new Scanner(System.in); System.out.println("请输入年月日(用逗号分隔):"); String []arr = sc.nextLine().split(","); int in_year =Integer.parseInt(arr[0]) ; int in_month = Integer.parseInt(arr[1]); int in_day = Integer.parseInt(arr[2]); //System.out.printf("%d,%d,%d\n",in_year,in_month,in_day); //建立一个日期数组,默认车辆均由当年一月一日制造 int day[] = {
0,31,59,90,120,151,181,212,243,273,304,334}; if(in_year < this.m_year) {
System.out.println("输入有误"); return 0; } else {
int res = (in_year-this.m_year)*365 + day[in_month] + in_day; return res; } } }
讯享网
运行结果
(2)有矩形和立方体类,根据类图编写代码。编写测试类生成矩形和立方体对象,调用计算和显示方法。
代码
讯享网public class Text7_2 {
public static void main(String[] args) {
/* 有矩形和立方体类,根据类图编写代码。编写测试类 * 生成矩形和立方体对象,调用计算和显示方法。 * */ testRect test1 = new testRect(); test1.test(); } } //测试类 class testRect {
public void test() {
Box box1 = new Box(3, 4, 5); box1.print(); } } //立方体类 class Box extends Rectangle {
private double m_height; public Box(double length,double width,double height) {
super(length,width); this.m_height = height; } public double getM_height() {
return m_height; } //面积 public double box_area() {
double area = 2*super.m_length*super.m_width+2*super.m_length*this.m_height+ 2*super.m_width*this.m_height; return area; } //周长 public double volume() {
return this.m_height*this.m_length*this.m_width; } public void print() {
System.out.println("立方体的表面积为:"+this.box_area()+"立方体的体积:"+this.volume()); super.print(); } } //矩形类 class Rectangle {
protected double m_length; protected double m_width; public Rectangle() {
} public Rectangle(double length,double width) {
this.m_length = length; this.m_width = width; } public double getM_length() {
return m_length; } public double getM_width() {
return m_width; } //面积 public double area() {
return this.m_length*this.m_width; } //周长 public double perimeter() {
return 2*(this.m_length+this.m_width); } public void print() {
double area = area(); double perimeter = perimeter(); System.out.println("矩形的面积为:"+ area + "。周长为:"+ perimeter); } }
运行结果


(3)测试类Test含有main方法,方法中创建品牌为huawei的对象,调用call方法和read方法
根据UML图编写4个类:
1、phone抽象类,含有抽象方法打电话
2、Ebook接口,含有抽象方法read()。
3、PadPhone继承了Phone抽象类并实现了Ebook接口
call方法输出:品牌的手机在打电话
read方法输出:品牌的电子书在阅读4、测试类Test含有main方法,方法中创建品牌为huawei的对象,调用call方法和read方法
代码
public class Text7_3 {
public static void main(String[] args) {
/* 根据UML图编写4个类: * 1、phone抽象类,含有抽象方法打电话 * 2、Ebook接口,含有抽象方法read()。 * 3、PadPhone继承了Phone抽象类并实现了Ebook接口 * call方法输出:品牌的手机在打电话 * read方法输出:品牌的电子书在阅读 * 4、测试类Test含有main方法,方法中创建品牌为huawei的对象,调用call方法和read方法 * */ testPadPhone test = new testPadPhone();//在堆区开辟数据,由jvm虚拟机实现回收资源 test.main(args); } } //编写测试类 class testPadPhone {
public void main(String [] args) {
PadPhone p1 = new PadPhone("HuaWei"); p1.Call(); p1.read(); } } //编写padPhone类 class PadPhone extends Phone implements Ebook {
private String m_brand; public PadPhone(String brand) {
this.m_brand = brand; } public void Call() {
System.out.println(this.m_brand+"品牌的手机在打电话。"); } public void read() {
System.out.println(this.m_brand+"品牌的电子书在阅读。"); } } //编写Ebook的接口 interface Ebook {
abstract void read(); } //编写phone的抽象类 abstract class Phone {
abstract void Call(); }
运行结果

初学java,代码多有不足,如有错误,非常感谢你的指正。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/23668.html