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

作业 专栏收录该内容
1 篇文章 0 订阅

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){
        super(wh,we);
        this.pa = pa;
    }

}

class Truck extends Vehicle{
    private int pa;
    private int pay;
	public void print() {
		System.out.println("truck:");
		super.print();
		System.out.println("Manned number:" + pa + " Bit");
		System.out.println("Capacity:" + pay + ".0"+ " Tons");
	}

    public int getPa() {
        return pa;
    }

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

    public int getPay() {
        return pay;
    }

    public void setPay(int pay) {
        this.pay = pay;
    }

    public Truck(int wh, int we, int pa, int pay){
        super(wh,we);
        this.pa = pa;
        this.pay = pay;
    }

}

public class Main {
	public static void main(String[] args) {
	    Scanner scan = new Scanner(System.in);
		int v1 = scan.nextInt();
		int v2 = scan.nextInt();
		
		int c1 = scan.nextInt();
		int c2 = scan.nextInt();
		int c3 = scan.nextInt();
		
		int t1 = scan.nextInt();
		int t2 = scan.nextInt();
		int t3 = scan.nextInt();
		int t4 = scan.nextInt();
		
		Vehicle ve1 = new Vehicle(v1, v2);
		ve1.show();
		ve1.print();
		Car car1 = new Car(c1, c2, c3);
		car1.print();
		Truck tr1 = new Truck(t1, t2, t3, t4);
        tr1.print();
        //scan.close();
	}

}

-题目2

定义一个father类,类中有getInfor方法,用于返回“this is father ”加上用户从键盘输入的信息,再定义一个son类,
继承father类,重写getInfor方法,用于返回“this is son”加上用户从键盘输入的信息。

import java.util.Scanner;;
//write code here
class father{
    public String name;
    public String getInfor(String str){
        this.name = "this is father " + str;
        return this.name;
    }
}


class son extends father{
    public String getInfor(String str){
        this.name = "this is son " + str;
        return this.name;
    }
}
public class Main {
    public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		father f = new father();
		System.out.println(f.getInfor(str));
		son s = new son();
		System.out.println(s.getInfor(str));
    }
}

居然还有彩色的代码块块儿~
我是新人,欢迎大家批评指正~

  • 4
    点赞
  • 0
    评论
  • 3
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页

打赏作者

nefu_fcy

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值