无法在Java中解析符号。我想在第一个类中运行第二个类的代码。

huangapple go评论112阅读模式
英文:

Cannot resolve symbol in Java. I want to run the code of second class in the first class

问题

以下是您提供的内容的中文翻译部分:

第一个类的代码:

package com.company;

public class Email {

    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String alternateEmail;
    private int capacityMailbox;

    // 构造函数用于接收名字和姓氏
    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
        System.out.println(em);
    }// 询问部门

    // 生成随机密码

    // 设置邮箱容量

    // 设置备用电子邮件

    // 更改密码

}

第二个类:

package com.company;

public class EmailApp {

    public static void main(String[] args) {
        // 在这里编写您的代码
        Email em = new Email("John", "Smith");
    }
}

屏幕截图:

无法在Java中解析符号。我想在第一个类中运行第二个类的代码。
无法在Java中解析符号。我想在第一个类中运行第二个类的代码。

谢谢

英文:

I am learning Java from tutorials, and recently I faced with one problem. I need to create several classes, and I need to read all that classes through first class. Already I passed that tutorial where I understood everything and tried that thing. But now I want to create a real world app on Java, I did the exact thing which was showing in tutorial, but I am getting that error. Please, correct me where I have mistaken

Code of first class:

package com.company;

public class Email {


    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String alternateEmail;
    private int capacityMailbox;

    // Constructor to recieve the first name and last name
    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
        System.out.println(em);
    }// Ask for the department

    // Generate a random password

    // Set the mailbox capacity

    // Set the alternate email

    // Change the password


}

Second one:

package com.company;

public class EmailApp {

    public static void main(String[] args) {
	// write your code here
        Email em = new Email("John", "Smith");
    }
}

Screenshotes:

无法在Java中解析符号。我想在第一个类中运行第二个类的代码。
无法在Java中解析符号。我想在第一个类中运行第二个类的代码。

Thanks

答案1

得分: 1

以下是翻译好的内容:

你可以在构造函数中使用 System.out.println(this)。这意味着你会将创建的实例打印到控制台上。还要不要忘记重写 toString() 方法 - 因为如果你不重写它,你会得到带有哈希码的对象名称。

希望这段代码能帮助你达到目标:

public class Email {
    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String alternateEmail;
    private int capacityMailbox;

    // 用于接收名字和姓氏的构造函数
    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
        System.out.println(this);
    }
    // 询问部门
    // 生成随机密码
    // 设置邮箱容量
    // 设置备用邮箱
    // 更改密码

    @Override
    public String toString() {
        return "Email{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

另一种解决方案。只需重写 Email 类的 toString 方法。并在主方法中打印 em

public class Email {
    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String alternateEmail;
    private int capacityMailbox;

    // 用于接收名字和姓氏的构造函数
    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
    // 询问部门
    // 生成随机密码
    // 设置邮箱容量
    // 设置备用邮箱
    // 更改密码

    @Override
    public String toString() {
        return "Email{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

主类:

public class EmailApp {
    public static void main(String[] args) {
    // 在这里编写你的代码
        Email em = new Email("John", "Smith");
        System.out.println(em);
    }
}
英文:

You can use System.out.println(this) in constructor. It means that you will print created instance to console. And also don't forget to override toString() method - because if you don't override it, you will get object's name with hashcode.

Hope this code will help you to reach the goal:

public class Email {
    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String alternateEmail;
    private int capacityMailbox;

    // Constructor to recieve the first name and last name
    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
        System.out.println(this);
    }
    // Ask for the department
    // Generate a random password
    // Set the mailbox capacity
    // Set the alternate email
    // Change the password

    @Override
    public String toString() {
        return "Email{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

Another solution. Just override toString of Email class. And put printing of em to main method.

public class Email {
    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String alternateEmail;
    private int capacityMailbox;

    // Constructor to recieve the first name and last name
    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
    // Ask for the department
    // Generate a random password
    // Set the mailbox capacity
    // Set the alternate email
    // Change the password

    @Override
    public String toString() {
        return "Email{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

Main class:

public class EmailApp {
    public static void main(String[] args) {
    // write your code here
        Email em = new Email("John", "Smith");
        System.out.println(em);
    }
}

答案2

得分: 0

你的Email类构造函数中没有名为em的变量。
你应该将System.out.println(em)放入你的EmailApp类的Main方法中。

英文:

The constructor of your Email Class does not have a variable called em.
You should put System.out.println(em) into the Main method of your EmailApp class.

答案3

得分: 0

以下是翻译好的部分:

"The object reference 'em'在第一个类Email中没有范围。

做以下两个更改并比较您的代码。您将了解您的错误之处。

//-----------------------------------------------------更改 1
public Email(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;

}

在第二个类中:

//---------------------------------------------更改 2
public class EmailApp {

public static void main(String[] args) {
// 写下您的代码
    Email em = new Email("John", "Smith");
    System.out.println(em.firstname);
    System.out.println(em.lastname);
}

}

并且暂时将所有成员变量设为public。当您理解“获取器和设置器”概念后,您将将其更改为private。"

英文:

The object reference "em" has no scope in first class i.e. class Email.

make these following two changes and compare your code. you will understand where you went wrong.

//-----------------------------------------------------change 1
  public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
      
    }

and in second class:

//---------------------------------------------change 2
public class EmailApp {

    public static void main(String[] args) {
    // write your code here
        Email em = new Email("John", "Smith");
        System.out.println(em.firstname);
        System.out.println(em.lastname);
    }

}

and make all member variables public for now. You will make it private when you understand "getters and setters" concept.

huangapple
  • 本文由 发表于 2020年10月1日 19:53:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64154855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定