package com.trainsports; public class trains { public int people = 0; public trains(){ System.out.print("运输方式选择:"); } /*public int getPeople() { return people; } public void setPeople(int people) { this.people = people; }*/ public void way(){ System.out.println("各种运输方式!"); } } package com.trainsports; public class airplane extends trains { //trains air = new airplane(); String way2 = "飞机"; public airplane(){ System.out.println(way2); System.out.println("飞机运输方式!"); people = 200; System.out.println("运输人数:"+people); } public void way(){ System.out.println("在天上飞行运输!"); } } package com.trainsports; public class bike extends trains { //trains b = new bike(); public bike(){ System.out.println("自行车赶路方式!"); people = 2; System.out.println("运输人数:"+people); } public void way(){ System.out.println("人力蹬车运输方式!"); } } package com.trainsports; public class boat extends trains { //trains bo = new boat(); public boat(){ System.out.println("轮船运输方式!"); people=500; System.out.println("运输人数:"+people); } public void way(){ System.out.println("水中运输方方式!"); } } package com.trainsports; public class bus extends trains { //trains b= new bus(); public bus(){ System.out.println("公交运输方式"); people = 40; System.out.println("运输人数:"+people); } public void way(){ System.out.println("公路运输方式!"); } } package com.trainsports; import java.util.Scanner; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); Initial in = new Initial(); while(true){ System.out.println("请选择你要查询的交通工具:"); System.out.println("1、自行车 2、公交车 3、轮船 4、飞机 5、退出"); int temp = input.nextInt(); if(temp == 5) { System.out.print("退出程序"); break; } else{ in.chiose(temp); } } input.close(); } public void chiose(int temp){ switch(temp){ case 1: trains bike = new bike(); bike.way(); break; case 2: trains bus = new bus(); bus.way(); break; case 3: trains boat = new boat(); boat.way(); break; case 4: trains airplane = new airplane(); airplane.way(); break; default: System.out.println("输入出错,请重新输入:"); break; } } } 大概就这么多了 字类不能继承父类的私有成员变量 但是我要改变他 使用setter时 建立一个父类的对象就会进入死循环啊 怎么做啊
我重跑了你的代码,把people改成了private,然后解注释了getter和setter,后面子程序用getter和setter代替了people,没有发现死循环。不知道你的代码在什么地方出现了问题。