java:无法从静态上下文中引用的非静态变量
java: non-static variable this cannot be referenced from a static context
我有错误java:非静态变量在编译行 Man m1 = new Man("a1", "b1", 中的代码时无法从静态上下文中引用11);如何解决?
public class Solution
{
public static void main(String[] args)
{
//create two object of every class here
Man m1 = new Man("a1", "b1", 11);
Man m2 = new Man("a2", "b2", 12);
Woman w1 = new Woman("a11", "b11", 13);
Woman w2 = new Woman("a22", "b22", 14);
//output them to screen here
System.out.println(m1.name + " " + m1.age + " " + m1.address);
System.out.println(m2.name + " " + m2.age + " " + m2.address);
System.out.println(w1.name + " " + w1.age + " " + w1.address);
System.out.println(w2.name + " " + w2.age + " " + w2.address);
}
//add your classes here
public class Man
{
private String name;
private String address;
private int age;
public Man(String name, String address, int age)
{
this.name = name;
this.address = address;
this.age = age;
}
}
}
}
一种方法
将 Man
class 声明为静态的,您将能够从 main()
中访问它,它也是静态的(不依赖于 [=27= 的任何实例) ] Solution
):
public static class Man
另一种方法
我们也可以保留 class Man
非静态并创建一个实例级工厂方法,它将创建 Man
:
的实例
public class Solution {
public static void main(String[] args) {
//create two object of every class here
Solution solution = new Solution();
Man m1 = solution.createMan("a1", "b1", 11);
Man m2 = solution.createMan( "a2", "b2", 12);
//output them to screen here
System.out.println(m1.name + " " + m1.age + " " + m1.address);
System.out.println(m2.name + " " + m2.age + " " + m2.address);
}
Man createMan(String name, String address, int age) {
return new Man(name, address, age);
}
//add your classes here
public class Man {
private String name;
private String address;
private int age;
private Man(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
}
}
public class Main {
public static void main(String[] args) {
// write your code here
VerySimple alice = new VerySimple();
}
class VerySimple {
private int age;
private String name;
//constructor
VerySimple(int age, String name) {
this.age = age;
this.name = name;
}
//getter method
int getAge() {
return age;
}
//setter method
public void setAge(int a) {
age = a;
}
//getter
String getName() {
return name;
}
}
}
我有错误java:非静态变量在编译行 Man m1 = new Man("a1", "b1", 中的代码时无法从静态上下文中引用11);如何解决?
public class Solution
{
public static void main(String[] args)
{
//create two object of every class here
Man m1 = new Man("a1", "b1", 11);
Man m2 = new Man("a2", "b2", 12);
Woman w1 = new Woman("a11", "b11", 13);
Woman w2 = new Woman("a22", "b22", 14);
//output them to screen here
System.out.println(m1.name + " " + m1.age + " " + m1.address);
System.out.println(m2.name + " " + m2.age + " " + m2.address);
System.out.println(w1.name + " " + w1.age + " " + w1.address);
System.out.println(w2.name + " " + w2.age + " " + w2.address);
}
//add your classes here
public class Man
{
private String name;
private String address;
private int age;
public Man(String name, String address, int age)
{
this.name = name;
this.address = address;
this.age = age;
}
}
}
}
一种方法
将 Man
class 声明为静态的,您将能够从 main()
中访问它,它也是静态的(不依赖于 [=27= 的任何实例) ] Solution
):
public static class Man
另一种方法
我们也可以保留 class Man
非静态并创建一个实例级工厂方法,它将创建 Man
:
public class Solution {
public static void main(String[] args) {
//create two object of every class here
Solution solution = new Solution();
Man m1 = solution.createMan("a1", "b1", 11);
Man m2 = solution.createMan( "a2", "b2", 12);
//output them to screen here
System.out.println(m1.name + " " + m1.age + " " + m1.address);
System.out.println(m2.name + " " + m2.age + " " + m2.address);
}
Man createMan(String name, String address, int age) {
return new Man(name, address, age);
}
//add your classes here
public class Man {
private String name;
private String address;
private int age;
private Man(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
}
}
public class Main {
public static void main(String[] args) {
// write your code here
VerySimple alice = new VerySimple();
}
class VerySimple {
private int age;
private String name;
//constructor
VerySimple(int age, String name) {
this.age = age;
this.name = name;
}
//getter method
int getAge() {
return age;
}
//setter method
public void setAge(int a) {
age = a;
}
//getter
String getName() {
return name;
}
}
}