如何将用户输入值传递给抽象的私有变量 类 (JAVA)
How to pass user input values to private variables in abstract classes (JAVA)
我正在尝试 Object 面向编程的过去纸质问题。
我需要将用户输入值传递给抽象 class 中的私有变量。但是由于无法实例化抽象classes,我无法将用户输入传递给抽象class“房间”中的变量。那么如何将用户输入值传递给抽象[=19中的私有变量=] ?我不知道用户会 select 哪种房间套餐类型(标准或豪华)。所以我也无法实例化 child class 。谁能帮忙?
注意:所有变量都应通过设置和获取方法访问。
public abstract class Room{
private char roomType;
private int roomNumber;
private int numOfDays;
private char roomPackageType;
public void setRoomType(char roomType){
this.roomType = roomType;
}
public char getRoomType(){
return this.roomType;
}
public void setRoomNumber(int roomNumber){
this.roomNumber = roomNumber;
}
public int getRoomNumber(){
return this.roomNumber;
}
public void setNumOfDays(int numOfDays){
this.numOfdays = numOfDays;
}
public int getNumOfDays(){
return this.numOfdays;
}
public void setRoomPackageType(char roomPackageType){
this.roomPackageType = roomPackageType;
}
public char getRoomPackageType(){
return this.roomPackageType;
}
public abstract void callPayment(int numOfDays,String customerType,String roomPackageType);
}
public class Standard extends Room{
public abstract void callPayment(int numOfDays,String customerType,String roomPackageType){
//code
}
}
public class Deluxe extends Room{
public abstract void callPayment(int numOfDays,String customerType,String roomPackageType){
//code
}
}
import java.util.Scanner;
import java.util.*;
public class Q2{
public static void main(String[] args){
Customer c1 = new Customer();
Scanner sc = new Scanner(System.in);
System.out.println("Please specify whether you are a local customer(L) or a foreign customer(F)");
char customerType = sc.next().charAt(0);
if(customerType != 'L' && customerType != 'F')
throw new InputMismatchException("Input must be either 'F' or 'L'");
c1.setCustomerType(customerType);
System.out.println("Enter your NIC number");
String idNumber = sc.nextLine();
c1.setIdNumber(idNumber);
System.out.println("Enter the package you would like to purchase");
char roomPackageType = sc.next().charAt(0);
Room.setRoomPackageType(roomPackageType);
}
}
您可以尝试在 roomPackageType
被选为抽象的基础上有条件地实例化 child classes
class 方法只能通过继承它的 classes 访问.
System.out.println("Enter the package you would like to purchase('D' for Deluxe or 'S' for Standard)");
char roomPackageType = sc.next().charAt(0);
if(roomPackageType.equals ('D')) {
Deluxe dlx = new Deluxe();
dlx.setRoomPackageType(roomPackageType);
}
else if(roomPackageType.equals('S')) {
Standard std = new Standard();
std.setRoomPackageType(roomPackageType);
}
else{
System.out.println("Invalid Room Package!");
}
我正在尝试 Object 面向编程的过去纸质问题。 我需要将用户输入值传递给抽象 class 中的私有变量。但是由于无法实例化抽象classes,我无法将用户输入传递给抽象class“房间”中的变量。那么如何将用户输入值传递给抽象[=19中的私有变量=] ?我不知道用户会 select 哪种房间套餐类型(标准或豪华)。所以我也无法实例化 child class 。谁能帮忙? 注意:所有变量都应通过设置和获取方法访问。
public abstract class Room{
private char roomType;
private int roomNumber;
private int numOfDays;
private char roomPackageType;
public void setRoomType(char roomType){
this.roomType = roomType;
}
public char getRoomType(){
return this.roomType;
}
public void setRoomNumber(int roomNumber){
this.roomNumber = roomNumber;
}
public int getRoomNumber(){
return this.roomNumber;
}
public void setNumOfDays(int numOfDays){
this.numOfdays = numOfDays;
}
public int getNumOfDays(){
return this.numOfdays;
}
public void setRoomPackageType(char roomPackageType){
this.roomPackageType = roomPackageType;
}
public char getRoomPackageType(){
return this.roomPackageType;
}
public abstract void callPayment(int numOfDays,String customerType,String roomPackageType);
}
public class Standard extends Room{
public abstract void callPayment(int numOfDays,String customerType,String roomPackageType){
//code
}
}
public class Deluxe extends Room{
public abstract void callPayment(int numOfDays,String customerType,String roomPackageType){
//code
}
}
import java.util.Scanner;
import java.util.*;
public class Q2{
public static void main(String[] args){
Customer c1 = new Customer();
Scanner sc = new Scanner(System.in);
System.out.println("Please specify whether you are a local customer(L) or a foreign customer(F)");
char customerType = sc.next().charAt(0);
if(customerType != 'L' && customerType != 'F')
throw new InputMismatchException("Input must be either 'F' or 'L'");
c1.setCustomerType(customerType);
System.out.println("Enter your NIC number");
String idNumber = sc.nextLine();
c1.setIdNumber(idNumber);
System.out.println("Enter the package you would like to purchase");
char roomPackageType = sc.next().charAt(0);
Room.setRoomPackageType(roomPackageType);
}
}
您可以尝试在 roomPackageType
被选为抽象的基础上有条件地实例化 child classes
class 方法只能通过继承它的 classes 访问.
System.out.println("Enter the package you would like to purchase('D' for Deluxe or 'S' for Standard)");
char roomPackageType = sc.next().charAt(0);
if(roomPackageType.equals ('D')) {
Deluxe dlx = new Deluxe();
dlx.setRoomPackageType(roomPackageType);
}
else if(roomPackageType.equals('S')) {
Standard std = new Standard();
std.setRoomPackageType(roomPackageType);
}
else{
System.out.println("Invalid Room Package!");
}