调用构造函数时收到错误

时间:2015-10-27 00:27:54

标签: java constructor

我在代码中扫描后到达此部分时收到此错误:

  

Project2_JoshuaLucas [,0,0,0x0,无效,布局= java.awt.FlowLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小= java.awt.Dimension中[宽度= 350,高度= 200]]

Project2_JoshuaLucas selection = new Project2_JoshuaLucas("", "", "", "", "", "", "", 0.00);

Object source = event.getSource();
if (source == cabin1)
{
  cabin1.setBackground(Color.darkGray);
  cabin2.setBackground(Color.gray);
  cabin3.setBackground(Color.gray);
  cabin4.setBackground(Color.gray);
  cabin5.setBackground(Color.gray);
  cabin6.setBackground(Color.gray);
  cabin7.setBackground(Color.gray);
  cabin8.setBackground(Color.gray);
  cabin9.setBackground(Color.gray);
  cabin10.setBackground(Color.gray);
  suite1.setBackground(Color.red);
  suite2.setBackground(Color.red);

  System.out.println("Your choice is Cabin 11-1, would you like to designate this as your room?");
  info1 = scan_in.nextLine();
  info1 = info1.toLowerCase();

 if ( info1.equals ("yes") || info1.equals ("y"))
{
   continues=true;   
   System.out.println("Please enter the number of people in your cabin (*Maximum number of people is 2*)");
   cabin_people = scan_in.nextInt();
   scan_in.nextLine();


   while(continues)
   {
     switch (cabin_people)
     {
     case 1:
       System.out.println("There is one passenger within the cabin. (You will pay an EXTRA 45% because of the empty passenger slot)");
       continues=false;
       onepassenger=true;
       break;
     case 2:
       System.out.println("There are two passenger within this cabin.");
       continues=false;
       twopassenger=true;
       break;
     default:
       System.out.println("Please try again. Remember, the maximum amount of passengers allowed is 2.");
       System.out.println("How many passengers are staying within this cabin?");
       cabin_people=scan_in.nextInt();
       scan_in.nextLine();
       continues=true;

 }//Closes the Switch
 }//Closes the while(continues) loop

 while(onepassenger)
 {
   System.out.println("Please state your FIRST name: ");
   fname1=scan_in.nextLine();
   System.out.println();
   System.out.println("Please state your LAST name: ");
   lname1=scan_in.nextLine();

   onepassenger=false;
   Project2_JoshuaLucas passenger1 = new Project2_JoshuaLucas (fname1, lname1, "", "", "", "", "", 0.00);
   System.out.println(passenger1);
 } //Closes while(1passenger)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

你的toString()方法正在打印对象数据中的所有内容。

答案 1 :(得分:0)

java中的每个类都扩展了 Object 类:

public class Example{ //extends the default class Object

}
  • 对象类有11种方法see here

    其中一个是 toString()方法

    通过调用 toString()创建类时,实际上是使用 Object 类中的方法,该方法默认返回表示类本身的String。

  • 方式1)

如果您想更改toString()以返回您想要的内容 @Override [Override] it。这就是在java.So中完成的方式:

    public class Example{

        @Override
        public String toString(){

         return "I am naughty";

       }

     }

提到当您使用覆盖时,通过将方法替换为您的方法来隐藏超类中的方法。

  • 方式2)

    您可以将自己的方法添加到课程中:

     public class Example{
    
    
       public String  getName(){
    
         return name;
       }
    
    
       public  String getLastName(){
    
         return lastName;
    
      }
     }