我遇到方法问题.任何帮助,将不胜感激:) [英] I am having a method issue. Any help would be appreciated:)

查看:95
本文介绍了我遇到方法问题.任何帮助,将不胜感激:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了4个类以及几个字段,构造函数和方法.

类:公共类CalculateRectangle,公共静态类Perimeter扩展CalculateRectangle,公共静态类Area扩展CalculateRectangle,公共静态类Volume扩展CalculateRectangle

字段:私有静态int长度;私有static int宽度;私有static int高度;私有静态整数p;私有静态整数a;私有静态int v;私有int外围;静态整数p = 0;私人int区域; static int a = 0;私人int量;静态整数v = 0;

构造函数:public CalculateRectangle(){length = 0;宽度= 0;高度= 0; },公共外围(){super(); } public Area(){super(); }公共卷(){super(); }

但是我在获取方法来调用变量时遇到了一些麻烦.谁能告诉我该如何做才能使它继续工作并保持继承形式?

I came up with 4 classes and several fields, constructors, and methods.

Classes: public class CalculateRectangle, public static class Perimeter extends CalculateRectangle, public static class Area extends CalculateRectangle, public static class Volume extends CalculateRectangle

Fields : private static int length; private static int width; private static int height; private static int p; private static int a; private static int v; private int perimeter; static int p = 0; private int area; static int a = 0; private int volume; static int v = 0;

Constructors: public CalculateRectangle (){ length = 0; width = 0; height = 0; }, public Perimeter () { super(); } public Area () { super(); }public Volume () { super(); }

But I''m having some trouble getting methods to call variables. Would any one tell me what I could do to make it work and still maintain inheritance form?

*/
package calculaterectangle;

/**
 *
 * @author Janie
 */
import java.util.Scanner;

public class CalculateRectangle {

    //class fields
    private static int length;
    private static int width;
    private static int height;
    private static int p;
    private static int a;
    private static int v;
    
    //constructor
    public CalculateRectangle (){
        length = 0;
        width = 0;
        height = 0;               
    }
            
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
        System.out.print("Enter length of rectangle         : ");
        String length = sc.next();
        System.out.print("Enter width of rectangle          : ");
        String width = sc.next();
        System.out.print("Enter height of rectangular prism : ");
        String height = sc.next();
       //method
       p = Perimeter.perimeter.setPerimeter(perimeter);
       a = Area.area.setArea(area);
       v = Volume.volume.setVolume(volume);
       
    System.out.println("perimeter:   " + p + "\n");
    System.out.println("Base Area:   " + a + "\n");
    System.out.println("Volume   :   " + v + "\n");
    
    System.out.print("Continue? (y/n): ");
    choice = sc.nextLine();
    System.out.println();
    }}
      public static class Perimeter extends CalculateRectangle {
         //class fields
         private int perimeter; 
         static int p = 0;
         //constructor
         public Perimeter () {
         super();
         }       
        public class perimeter extends Perimeter {
           //constructor
            public perimeter () {
             super(); }
            //method
           public void setPerimeter(){   
            perimeter = 2 * (length + width);
            p = perimeter;
            }
        }
     }
    
    public static class Area extends CalculateRectangle {
        //class fields
        private int area; 
        static int a = 0;
        //constructor
        public Area () {
        super(); }
        public class area extends Area {
             //constructor 
            public area () {
                super();}
            //method
            public void setArea() {
                area = length * width;
                a = area;
            }
            }
        
        }
  
    public static class Volume extends CalculateRectangle {
        //class fields
        private int volume; 
        static int v = 0;  
        //constructor
        public Volume () {
        super();   }
        public class volume extends Volume {
               //constructor 
              public volume() {
                  super();}
              //method
              public void setVolume() {
                  volume = length * width * height;
                  v = volume;
              }
              }  
        }
       
        
    }

推荐答案

很简单,您不能在静态类中使用非静态类变量/属性.
It is simple, you cannot use a non-static class variable/property from within a static class.


除了Vitaly的正确答案:

实例(非静态)方法实际上与静态方法完全相同,但有一个附加功能:它们具有一个称为"this"的附加参数,该参数未显示在参数列表中,但可以在实现中使用.方法.这是对声明该方法的类型的某些对象的实例的引用.这样,所有实例方法都可以处理该类的某个实例,因此,通过使用此"this"参数,可以在其实现中使用所有非静态成员.

相反,静态方法没有有关任何特定实例的信息,因为它们不是用实例而是用类型来调用的.因此,它们只能访问整个类共有的成员,而不能访问特定于任何实例的成员,也就是说,它们只能使用静态数据和其他静态方法,实际上只能使用静态成员.

—SA
In addition to a correct answer by Vitaly:

Instance (non-static) methods are actually exactly the same as the static one, with one additional feature: they have one extra parameter called "this", which is not shown in a parameter list, but can be used inside the implementation of a method. This is a reference to the instance of some object of the type declaring the method. This way, all instance methods deal with some instance of the class, so all non-static members can be use in its implementation, thank to this "this" parameter.

In contrast, static methods has no information about any particular instance, because they are called not with instance but with the type. So, they can access only the members common for the whole class, not specific to any instance, that is, they can only use static data and other static methods, effectively, only the static members.

—SA


看看您的问题,我建议您花几个小时来完成^ ]会花费很多时间.
Looking at your question I would suggest a few hours going through the Java Tutorials[^] would be time well spent.


这篇关于我遇到方法问题.任何帮助,将不胜感激:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆