【问题标题】:How can use a variable of a object which is created by the user ?(Java)如何使用用户创建的对象的变量?(Java)
【发布时间】:2020-07-10 14:01:15
【问题描述】:

我创建了一个类并将其留给用户来创建一个实例。该实例有一个构造函数,要求用户向实例输入值:-

public class perfo2{
     public int c;
     public int p;
     public int b;  
     public String n;
     perfo2(int c,int p,int b,String n){              //constructor
         this.c=c;
         this.p=p;
         this.b=b;
         this.n=n;
}

现在我有一些方法需要实例中的变量,例如:-


   public  int calculate(int c,int p,int b){
        int per= (int)((c+p+b/60*100)); 
        return per;
    }
   public  void  dis(int c,int p,int b,String n,int per){
        System.out.println("Name:"+n);
        System.out.println("Chemistry:"+c);
        System.out.println("Physics:"+p);
        System.out.println("Biology:"+b);
        System.out.println("Percentage:"+per+"%");

    } }

现在我希望这些方法能够实际访问对象的各种变量并使用它们。 我知道我对这些方法给出的论点是不能做到的,但是什么会呢?并且 如果我在代码本身中创建一个对象,我可以通过

轻松访问变量
michael.dis(michael.c,michael.p,michael.b,michael.n,michael.calculate(michael.c,michael.p,michael.b));

【问题讨论】:

  • 您可能只想将对象作为参数传递,如下所示 public void dis(perfo2 perfo) 然后您可以定义 perfo.n 或 perfo.c 等变量

标签: java input methods constructor arguments


【解决方案1】:

只需创建一个对象并使用它

perfo2 michael = new perfo2(c,p,b,n);

 michael.dis(michael.c,michael.p,michael.b,michael.n,michael.calculate(michael.c,michael.p,michael.b));

【讨论】:

    【解决方案2】:

    如果我理解正确的话;您希望能够访问在构造函数c, p, b, n 中设置的变量。您应该可以通过在每个变量上创建 getter 来做到这一点:

    public class perfo2 {
      public int c; // consider making the access modifier for c,p,b & n private
      public int p;
      public int b;
      public String n;
    
      perfo2(int c, int p, int b, String n) {   //constructor
        this.c = c;
        this.p = p;
        this.b = b;
        this.n = n;
      }
    
      public int getC() {
        return c;
      }
    
      public int getP() {
        return p;
      }
    
      public int getB() {
        return b;
      }
    
      public String getN() {
        return n;
      }
    }  
    
    // Create the object as such 
    perfo2 person1 = new perfo2(1,2,3,"my String");
    int c = person1.getC();
    int p = person1.getP();
    int b = person1.getB();
    String n = person1.getN();
    

    您可能还需要考虑将c,p,bn 的访问修饰符设为私有;因此无法从对象直接访问它。根据用例,您还可以使用person1.c

    【讨论】:

      【解决方案3】:

      我的评论有一点额外的代码,你可以像这个例子一样使用你的类。您可能可以将百分比添加到您的类变量中,但我不想弄乱您的逻辑

      public class Perfo2 {
         private int c;
         private int p;
         private int b;
         private String n;
      
         Perfo2(int c, int p, int b, String n) { // constructor
            this.c = c;
            this.p = p;
            this.b = b;
            this.n = n;
         }
      
         public int calculate(Perfo2 perfo2) {
            return (perfo2.c + perfo2.p + perfo2.b / 60 * 100);
         }
      
         public void dis(Perfo2 perfo2,int per) {
            System.out.println(perfo2);
            System.out.println("Percentage:" + per + "%");
         }
      
         @Override
         public String toString() {
            return String.format("Name: %s%nChemistry: %s%nPhysics: %s%nBiology: %s", this.n ,this.c,this.p,this.b);
         }
      
         public static void main(String[] args) {
            Perfo2 p = new Perfo2(10,6,5,"Mark");
            p.dis(p, 70);;
      
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        相关资源
        最近更新 更多