Java 如何使用set方法设置数据字段?

Java 如何使用set方法设置数据字段?,java,Java,在一个类中,我定义了一个setX方法 public class Point{ int x; int y; ... public int setX(int x){ this.x = x; } ... } 在另一个类中,我需要创建一个点对象,并将其x字段设置为1,我不知道该写什么。这是我写的 public class Lab1{ public static void main (String[] args){ Point a; a.x = new setX(1); ... } } 我应

在一个类中,我定义了一个setX方法

public class Point{
int x;
int y;
...

public int setX(int x){
this.x = x;
}

...
}
在另一个类中,我需要创建一个点对象,并将其x字段设置为1,我不知道该写什么。这是我写的

public class Lab1{
 public static void main (String[] args){
Point a;

a.x = new setX(1);
...
 }
}

我应该怎么做?

您需要初始化
点a尝试放置
点a=new Point()
然后您应该能够将其设置为x这应该是您的代码

public class Lab1{
public static void main (String[] args){
Point a = new Point();

a.setX(1);
System.out.println(a.x);//the value 1 should be displayed
}
如果你想知道你的类在构造函数中是什么样子的,这里有一个例子

public class Point{
int x;
int y;

public Point(int x, int y){
this.x = x;
this.y = y;
}

public void setX(int x){
this.x = x;
}

public void setY(int y){
this.y = y;
}

public int getX(){
return this.x;
}

}

public int getY(){
return this.y
}

您需要初始化
点a尝试放置
点a=new Point()
然后您应该能够将其设置为x这应该是您的代码

public class Lab1{
public static void main (String[] args){
Point a = new Point();

a.setX(1);
System.out.println(a.x);//the value 1 should be displayed
}
如果你想知道你的类在构造函数中是什么样子的,这里有一个例子

public class Point{
int x;
int y;

public Point(int x, int y){
this.x = x;
this.y = y;
}

public void setX(int x){
this.x = x;
}

public void setY(int y){
this.y = y;
}

public int getX(){
return this.x;
}

}

public int getY(){
return this.y
}

setter应该是空的,因为它们不返回任何值…对吗?因为它们只设置实例变量

public void setX(int x){
  this.x = x;
}
在Point类中,应该有一个构造函数,对吗?可以从类点创建新对象

你是怎么做到的

像这样:

Point someName=new Point()

使用构造函数设置值…不是您的示例

public class Lab1 {
    public static void main (String[] args){
        Point a = new Point(5);
    }
}
您的解决方案

public class Lab1 {
    public static void main (String[] args){
        Point a = new Point();
        a.setX(5);
    }
}

setter应该是空的,因为它们不返回任何值…对吗?因为它们只设置实例变量

public void setX(int x){
  this.x = x;
}
在Point类中,应该有一个构造函数,对吗?可以从类点创建新对象

你是怎么做到的

像这样:

Point someName=new Point()

使用构造函数设置值…不是您的示例

public class Lab1 {
    public static void main (String[] args){
        Point a = new Point(5);
    }
}
您的解决方案

public class Lab1 {
    public static void main (String[] args){
        Point a = new Point();
        a.setX(5);
    }
}

你应该这样写

class Point{
    int x;
    int y;

    public void setX(int x){
         this.x = x;
    }

    public int getX(){
        return this.x;
    }
}



public class Lab1 {

    public static void main(String[] args) {
        Point A = new Point();
        A.setX(1);      
        System.out.println(A.getX());

    }
}

你应该这样写

class Point{
    int x;
    int y;

    public void setX(int x){
         this.x = x;
    }

    public int getX(){
        return this.x;
    }
}



public class Lab1 {

    public static void main(String[] args) {
        Point A = new Point();
        A.setX(1);      
        System.out.println(A.getX());

    }
}

不能将
new
方法一起使用。您需要
。不能将
新建
方法一起使用。你需要
class
。等我把setter搞定后,你说得对。下一步我该怎么设置它的x字段呢?这是真的,实际上代码似乎有一些地方出了问题。如果你看我的答案,你似乎想给一个非斜体对象设置一个值。他没有构造函数,所以他不能放新的点(5),他需要先创建一个构造函数。“设定者应无效”,除非他们是工厂或建筑商的一部分(例如),然后返回对对象的引用总是很有帮助的,然后您可以让方法链接工作,例如
StringBuilder
,但这可能超出了问题的范围;)在我正确设置setter之后,您是对的。接下来我应该做什么来设置它的x字段?这是真的,实际上看起来有一些事情错误的代码。如果你看我的答案,看起来你是在试图为一个非斜体对象设置一个值。他没有构造函数,所以他不能提出新的观点。他需要创建一个构造函数来实现这一点。设置器应该是空的,除非它们是工厂或构建器的一部分(例如),然后返回对对象的引用总是有帮助的,然后你可以让方法链接工作,比如
StringBuilder
,但这可能超出了问题的范围;)这很完美。NJU rocks。这很完美。NJU rocks。这比我想要的多得多。谢谢你,先生。这比我想要的多得多。谢谢你,SiR