Java 在课堂上写一张椅子

Java 在课堂上写一张椅子,java,Java,写一个OfficeHair类 有两个实例变量 有一个静态int,用于跟踪Office头发的数量 有一个默认构造函数 有一个重载的构造函数–以两个数据成员作为输入 两个构造函数都需要添加到静态int 编写两行代码,说明如何生成OfficeHair的实例——一行使用默认构造函数,另一行使用重载构造函数 您不需要编写任何getter/setter 我需要知道如何编写两行代码来创建一个实例 public class OfficeChair { public string function c

写一个OfficeHair类

  • 有两个实例变量
  • 有一个静态int,用于跟踪Office头发的数量
  • 有一个默认构造函数
  • 有一个重载的构造函数–以两个数据成员作为输入
  • 两个构造函数都需要添加到静态int
编写两行代码,说明如何生成OfficeHair的实例——一行使用默认构造函数,另一行使用重载构造函数

您不需要编写任何getter/setter

我需要知道如何编写两行代码来创建一个实例

public class OfficeChair
{
    public string function chairSwivel;
    private int chairSoftness;
    static int chairCount = 0;
}   
  OfficeChair()
        {
        chairSwivel = “Yes”;
        chairSoftness = “Not Given”;
        chairCount = “Not Given”;
        }   
     public OfficeChair( string Str, int num1, static int num2)
     {
     chairSwivel = str
     chairSoftness = num1
     chairCount = num2
     }
}
public Class officeChairs {
    public static void main(String args[])
    {
        Chair officeChair = new OfficeChair(Yes, Very Soft, 8)
那将是

OfficeChair officeChair1=new OfficeChair();


哦,天哪。代码中有很多错误。那么

public class OfficeChair
{
    // `string` is not a type in Java
    // `function` is a made up keyword
    // chairSwivel should probably be a `boolean` typed variable
    public string function chairSwivel;
    private int chairSoftness;
    static int chairCount = 0;
} // Class definition ends

  // This is outside the class, see above }
  OfficeChair()
        {
        // Wrong quotes (for other literals as well)
        chairSwivel = “Yes”;
        // Type error - a string cannot be assigned to an integer variable.
        chairSoftness = “Not Given”;
        // Type error as per above, also fails to meet requirements
        chairCount = “Not Given”;            
        }
     // Please choose meaningful parameter/variable names
     public OfficeChair( string Str, int num1, static int num2)
     {
     // Missing semicolons
     chairSwivel = str
     chairSoftness = num1
     // Fails to meet requirements
     chairCount = num2
     }
} // Bogus, see first class-ending }

// Class should be `class` - all keywords in Java are lowercase.
// officeChairs is also mis-cased as class names should all be CamelCased.
// Only one class with a given name (or parent type if nested) can exist.
public Class officeChairs {
    public static void main(String args[])
    {
        // There is no Chair type defined
        // There is no Yes identifier in scope, string literals need quotes.
        // Very Soft (or even the less-incorrect "Very Soft") is still not
        // assignable to an integer. Missing semicolon.
        Chair officeChair = new OfficeChair(Yes, Very Soft, 8)
在所有这些之后(可能不是全部),使用:

new
操作符通过为新对象分配内存并返回对该内存的引用来实例化类。新操作符还调用对象构造函数


您应该用默认构造函数实例化一把椅子,用重载构造函数实例化一个实例。你只是在你的底线上做了后者。哦,我好吧,这是一个例子,谢谢,那么我该如何投票否决你,这是因为你问了你的问题,没有表现出你试图创建第二个例子,也没有澄清你的误解。没有表现出你的企图,你欺骗了自己一次宝贵的经历,你也阻止了我们看到你可能做错了什么。我相信你的问题在将来会变得更好,但请记住这一点,以备将来提问。我的尝试就是我编写的全部代码。。。。。。。正如你所知,编码是非常敏感的,我对java是新手,只是试着向有帮助的人学习与你的问题无关的东西,但是根据你的要求,你应该在两个构造函数中增加你的计数器。您不应该直接设置数字。你的代码也不会像这样工作
chairCount
是一个
整数(int)
。您不能将未指定的
字符串
分配给它,因为它的类型错误。
public class OfficeChair
{
    // `string` is not a type in Java
    // `function` is a made up keyword
    // chairSwivel should probably be a `boolean` typed variable
    public string function chairSwivel;
    private int chairSoftness;
    static int chairCount = 0;
} // Class definition ends

  // This is outside the class, see above }
  OfficeChair()
        {
        // Wrong quotes (for other literals as well)
        chairSwivel = “Yes”;
        // Type error - a string cannot be assigned to an integer variable.
        chairSoftness = “Not Given”;
        // Type error as per above, also fails to meet requirements
        chairCount = “Not Given”;            
        }
     // Please choose meaningful parameter/variable names
     public OfficeChair( string Str, int num1, static int num2)
     {
     // Missing semicolons
     chairSwivel = str
     chairSoftness = num1
     // Fails to meet requirements
     chairCount = num2
     }
} // Bogus, see first class-ending }

// Class should be `class` - all keywords in Java are lowercase.
// officeChairs is also mis-cased as class names should all be CamelCased.
// Only one class with a given name (or parent type if nested) can exist.
public Class officeChairs {
    public static void main(String args[])
    {
        // There is no Chair type defined
        // There is no Yes identifier in scope, string literals need quotes.
        // Very Soft (or even the less-incorrect "Very Soft") is still not
        // assignable to an integer. Missing semicolon.
        Chair officeChair = new OfficeChair(Yes, Very Soft, 8)