XiaoYUU
2015-06-26 12:04
//车的父类 package com.dadazuche; public abstract class Typeofcar { private String carname; private int price; private int sequence; public int getSequence() { return sequence; } public void setSequence(int sequence) { this.sequence = sequence; } public String getCarname() {//方法名 用get的方法来得到carname return carname; } public void setCarname(String carname) { this.carname = carname; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } } //货车的子类 package com.dadazuche; public class huoche extends Typeofcar { private int numofgoods; public huoche (int sequence, String carname, int price, int numofgoods) { // TODO Auto-generated method stub setSequence(sequence); setCarname(carname); setPrice(price); this.numofgoods=numofgoods; } public int getNumofgoods() { return numofgoods; } public void setNumofgoods(int numofgoods) { this.numofgoods = numofgoods; } public void output() { System.out.println("车:"+getSequence()+"\t"+"车型:"+getCarname()+"\t"+"价格:"+getPrice()+"一天\t"+"载货量:"+getNumofgoods()+"吨"); } } //客车的子类 package com.dadazuche; public class keche extends Typeofcar { private int numofpeople; public keche(int sequence, String carname, int price, int numofpeople) { // TODO Auto-generated method stub setSequence(sequence); setCarname(carname); setPrice( price); this.numofpeople=numofpeople; } public int getNumofpeople() { return numofpeople; } public void setNumofpeople(int numofpeople) { this.numofpeople = numofpeople; } public void output() { System.out.println("车:"+getSequence()+"\t"+"车型:"+getCarname()+"\t"+"价格:"+getPrice()+"一天\t"+"载人数:"+getNumofpeople()+"人"); } } //皮卡的子类 package com.dadazuche; public class pika extends Typeofcar { private int numofpeople; private int numofgoods; // TODO Auto-generated method stub public pika(int sequence, String carname, int price,int numofgoods,int numofpeople) { setSequence(sequence); setCarname(carname); setPrice( price); this.numofgoods=numofgoods; this.numofpeople=numofpeople; } public int getNumofpeople() { return numofpeople; } public void setNumofpeople(int numofpeople) { this.numofpeople = numofpeople; } public int getNumofgoods() { return numofgoods; } public void setNumofgoods(int numofgoods) { this.numofgoods = numofgoods; } public void output() { System.out.println("车:"+getSequence()+"\t"+"车型:"+getCarname()+"\t"+"价格:"+getPrice()+"一天\t"+"载货量:"+getNumofgoods()+"吨" +"\t"+"载人数:"+getNumofpeople()+"人"); } } //main的入口 package com.dadazuche; import java.util.Scanner; public class initial { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int choice=0,a=0,b=0,c=0; huoche obj1=new huoche(1,"雪铁龙",240,5); keche obj2=new keche(2,"大金龙",280,30); pika obj3=new pika(3,"皮卡",350,4,4); System.out.println("Do you want to use our DaDaZuChe to rent a car? Enter 1 for yes."); int num=in.nextInt(); if(num==1) { System.out.println("Welcome to DaDaZuChe."); } do {System.out.println("Please enter a number from 1 to 3(1 for viewing available cars, 2 for choosing car"+ " 3 for view total"); int num1=in.nextInt(); switch(num) { case 1: { obj1.output(); obj2.output(); obj3.output(); } case 2: { System.out.println("How many 雪铁龙 do you want to rent?");//可以加上租几天的雪铁龙的问题 a=in.nextInt(); System.out.println("How many 大金龙 do you want to rent?"); b=in.nextInt(); System.out.println("How many 皮卡 do you want to rent?"); c=in.nextInt(); } case 3: { int total=240*a+280*b+350*c; System.out.println("Your total is "+total); System.out.println("Do you want to rent another car? 1 for yes,2 for no"); choice=in.nextInt(); } } }while(choice==1); System.out.println("Thank you for choosing DaDaZuChe!!"); } }
各位大侠,这是我的code。只是一个简略版,还要加东西。我的问题是:我在main里面怎样输入我在子类里面输入的车的属性?是需要getter和setter吗?还有如果不行,是不是我想在main里引用子类都要在子类里面创建一个方法,而不用属性?方法是不是比属性要好用?
看问答区置顶的帖子,有分析,有大神指点
Java入门第二季 升级版
530653 学习 · 6091 问题
相似问题