作业系列一:方法的重写与继承

本文通过两个实例介绍Java中的方法重写和继承概念。第一个例子涉及Vehicle、Car和Truck类的创建,分别展示类的数据成员和相关输出函数。第二个例子定义了father和son类,son类重写了getInfor方法,展示了继承和方法重写的运用。
摘要由CSDN通过智能技术生成

Java中方法的重写与继承例题

  • 题目1

1.设计一个汽车类Vehicle,包含的数据成员有车轮个数wheels和车重weight。
2.小车类Car是它的公有派生类其中包含载人passenger。
3.卡车类Truck是Vehicle的公有派生类其中包含载人数passenger和载重量pay。
4.每个类都有相关数据的输出函数,用以测试构造函数调用顺序,以及成员变量初始化及数据输出等。

输入:
8 5
4 1 5
6 8 2 10
输出:
car:
Manned number:8 Bit
Capacity:5.0 Tons
a car:
Manned number:4 Bit
Capacity:1.0 Tons
Manned number:5 Bit
truck:
Manned number:6 Bit
Capacity:8.0 Tons
Manned number:2 Bit
Capacity:10.0 Tons

//package LA;
import java.util.Scanner;
class Vehicle{
    private int wh;
    private int we;
    
    public Vehicle(int wh,int we){
        super();
        this.wh = wh;
        this.we = we;
    }

    public void show() {
		System.out.println("car:");
	}

	public void print() {
		System.out.println("Manned number:" + wh + " Bit");
		System.out.println("Capacity:" + we + ".0" +  " Tons");
	}

    public int getWh() {
        return wh;
    }

    public void setWh(int wh) {
        this.wh = wh;
    }

    public int getWe() {
        return we;
    }

    public void setWe(int we) {
        this.we = we;
    }
}

class Car extends Vehicle{
    private int pa;
    
	public void print() {
		System.out.println("a car:");
		super.print();
		System.out.println("Manned number:" + pa + " Bit");
	}

    public int getPa() {
        return pa;
    }

    public void setPa(int pa) {
        this.pa = pa;
    }

    public Car(int wh, int we, int pa){
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值