对象和类的Java编码问题 [英] Problem with Java coding for objects and classes

查看:96
本文介绍了对象和类的Java编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我无法弄清楚以下内容出了什么问题
java文件.任何帮助将非常感激.我敢肯定,这很简单,我很茫然! :confused:

For some reason I can''t figure out what is wrong with the following
java file. Any help would be really appreciated. I am sure that it is something simple bit I am at a loss! :confused:

class Test {
  public static void main(String[] args) {
  A a = new A();
  a.print();
  }
}





class A {
  String s;

 A(String s){
   this.s = s;
}

public void print() {
  System.out.print(s);
  }
}

推荐答案

try::-D

try : :-D

class Test {
public static void main(String[] args) {
A a = new A("World of Warcraft!");
a.print();
}
}


只是尝试充实" Adrabi的答案:

您创建了A类,其构造函数将单个字符串作为输入.

然后,当您创建新A时,您没有传递任何内容.我假设您应该传递的内容来自参数列表,对吗?

您可能想要:

Just to try to "flesh out" Adrabi''s answer:

You created class A with a constructor that takes as input a single string.

Then, when you created your new A, you didn''t pass anything in. I assume what you were supposed to pass in was from the argument list, correct?

You may have wanted:

public static void main(String[] args) {
    for (String s: args)
    {
        A a = new A(s);
        a.print();
    }  
}


尝试这样的事情

公共课程测试{
公共静态void main(String [] args){
A a = new A();
a.print();
}
}

becoz,Java需要的主类是公共访问说明符
Try some thing like this

public class Test {
public static void main(String[] args) {
A a = new A();
a.print();
}
}

becoz, Java need main class is public access specifier


这篇关于对象和类的Java编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆