在主函数中创建对象实例。

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

Create instance of object in main function

问题

以下是您要翻译的内容:

我应该调用方法 set 来改变我创建的对象的属性,但是它给了我以下错误:

PrimerPrograma.java:34: 错误: 无法从静态上下文引用非静态变量 this

我尝试的代码是:

    public class PrimerPrograma{

      public class Punto{
         int x;          
         int y;

         public Punto(int lax, int lay){         
            x=lax;
            y=lay;       
         }
    
         public void set(int lax, int lay){                                   
            x=lax;
            y=lay;
         }

        public static void main(String[]args){
           Punto p=new Punto(5,5);
           p.set(8,8);
           System.out.println(p.x+p.y);
        }
    
    }

我如何创建 Punto 类的实例,以便我可以调用它的方法?

英文:

I am supposed to call the method set to make it change the attributes of the object I created, but is giving me the following error:

PrimerPrograma.java:34: error: non-static variable this cannot be
referenced from a static context

The code that I tried is:

    public class PrimerPrograma{

      public class Punto{
         int x;          
         int y;

         public Punto(int lax, int lay){         
            x=lax;
            y=lay;       
         }
    
         public void set(int lax, int lay){                                   
            x=lax;
            y=lay;
         }

        public static void main(String[]args){
           Punto p=new Punto(5,5);
           p.set(8,8);
           System.out.println(p.x+p.y);
        }
    
    }

How do I create an instance of the Punto class, so that I can call its methods?

答案1

得分: 1

把实例化、设置和System打印调用放在你的主函数里。我建议你逐步跟随一些关于基础知识的YouTube视频。这在刚开始会对你很有帮助。

英文:

Put the instantiation, set and System print call inside your main function. I recommend follow step-by-step some youtube videos about the basic. This will help you much in the beggining.

答案2

得分: 1

来自daniweb的内容:

非静态变量只在存在对象实例时才存在。如果你有一个静态方法,它无法访问类对象中的任何变量,除非它拥有该类的实例并使用它来访问变量。对于你的内部类,如果希望主方法能够引用它,请将public更改为static。

以下是可以正常工作的代码:

public class PrimerPrograma{

     public static void main(String []args){
        Punto p = new Punto(5, 5);
        p.set(8,8);
        System.out.println(p.x+p.y);
     }
     
     public static class Punto{
        int x;          
        int y;
        
        public Punto(int lax, int lay) {
            x = lax;
            y = lay;
        }

        public void set(int lax, int lay){
            this.x = lax;
            this.y = lay;
        }
    }
}

我建议阅读这个讨论。

英文:

From daniweb:
> A non-static variable only exists when there is an instance of an
> object. If you have a static method, it can NOT get to any variable in
> a class object unless it has an instance of that class and uses that
> to get to the variable. For your inner class, change public to static
> if you want the main method to be able to reference it.

This code should work:

public class PrimerPrograma{

     public static void main(String []args){
        Punto p = new Punto(5, 5);
        p.set(8,8);
        System.out.println(p.x+p.y);
     }
     
     public static class Punto{
        int x;          
        int y;
        
        public Punto(int lax, int lay) {
            x = lax;
            y = lay;
        }

        public void set(int lax, int lay){
            this.x = lax;
            this.y = lay;
        }
    }
}

I recommend reading this discussion.

huangapple
  • 本文由 发表于 2020年10月9日 14:35:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64275036.html
匿名

发表评论

匿名网友

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

确定