简体   繁体   English

类实例和静态方法之间的关系

[英]Relation between class instance and static methods

Is there any particular relation between class instance (eg variables that declared by static/final) and static methods(eg class methods) ? 类实例(例如,由static / final声明的变量)与静态方法(例如,类方法)之间是否存在任何特殊关系?

I hop you understand what i mean. 我希望你明白我的意思。

我认为您所谈论的是静态变量而不是类实例 (没有这个术语),我可以想到的是静态变量和静态方法之间的一种联系是,在您的静态方法中,您只能访问静态变量,而不能访问实例变量

Static and non-static methods/classes are entirely different beasts. 静态和非静态方法/类是完全不同的野兽。 Let's use an example of object-oriented programming. 让我们使用一个面向对象编程的例子。 Say I have a class called "Person", with a constructor called Person() , with the instance variables declared as myAge (set as 0), and a method called Birthday() : 假设我有一个名为“ Person”的类,其构造函数名为Person() ,其实例变量声明为myAge(设置为0),还有一个方法名为Birthday()

int myAge;

public Person(){
    myAge = 0;
}

public void Birthday(){
    myAge += 1;
}

In my main method, this class would be used like this: 在我的main方法中,此类的用法如下:

Person Sajjad = new Person();
Sajjad.Birthday();

When I create a new person, which is you, your age is 0, because our constructor's default values are 0 for myAge . 当我创建一个新的人即您时,您的年龄为0,因为myAge构造函数的默认值为0。 Once I apply the Birthday() method on you, your age goes up by one, through the increment. 一旦对您应用Birthday()方法,您的年龄就会增加1。

As for static methods, there's no object-oriented principles in it. 至于static方法,其中没有面向对象的原理。 There are uses for it.. I usually use it for simple math. 它有用途。我通常将其用于简单的数学运算。

public static double addValues(double a, double b){
    return a + b;
}

In my main : 在我的main

int sum;
sum = addValues(1, 2);
System.out.println(sum);

Output: 输出:

3

See how above there's no need to declare a new objects? 看看上面如何无需声明新对象? Static methods are easier to prototype, but in the long run I definitely go with object-oriented principles because it makes maintaining code in the long run so much easier. 静态方法更容易原型化,但是从长远来看,我肯定会采用面向对象的原则,因为从长远来看,它使维护代码变得更加容易。 Also, I don't need to clutter my main method with unnecessary lines of code. 另外,我不需要用不必要的代码行使我的主要方法混乱。

PS If the code is wrong, it's really just some pseudo code I whipped up. PS:如果代码错误,那实际上只是我鞭打过的一些伪代码。

Class instances are objects stored in memory and referenced through a variable of the Type of the class. 类实例是存储在内存中的对象,并通过类的Type变量进行引用。 Each element has its own state and its own methods. 每个元素都有其自己的状态和方法。 Ex: if we have a Apple class and a getColor non-static element: 例如:如果我们有一个Apple类和一个getColor非静态元素:

Apple a = new Apple("green");
Apple b = new Apple("red");
a.getColor();//green
b.getColor();//red

Static methods/variables are elements related to that class and acceded via the name of the class. 静态方法/变量是与该类相关的元素,并通过该类的名称进行访问。 You can't access static elements from an instance. 您无法从实例访问静态元素。 Ex: if we have a Apple class and a size static element: 例如:如果我们有一个Apple类和一个size静态元素:

Apple.tree; //Apple Tree

Here's class Apple 这是苹果课

class Apple {
    public static String tree = "Apple Tree"; // Class related
    private String color; //object instance related

    public Apple(String color) {
        this.color = color;
    }

    public String getColor() {
        return this.color;
    }
}

So, having this class color will depend on the instance. 因此,具有此类颜色将取决于实例。

Access: class instance (ie variables that declared by static/final) and static methods can work together without explicit need of object reference. 访问: class instance (ie variables that declared by static/final)static方法可以一起工作,而无需明确引用对象。 Whereas to access member variable (aka instance variables) you need to access via object reference. 而访问成员变量(又称实例变量)则需要通过对象引用进行访问。

class Test {
    static String str = "asdf";
    int num = 10;

    static void methodA() {
        System.out.println("Static variable direct access: " + str);
        System.out.println("Member variable access : " + new Test().num);
    }
}

Concurrency: Other difference is during concurrent access or thread-safety logic, class-level-lock can be taken via class-variables in static-methods and object-level-lock can be taken via member-variables in member-methods though you can mix-n-match ofcourse. 并发:其他不同的是并发访问或线程安全逻辑,在class-level-lock可以通过采取class-variablesstatic-methodsobject-level-lock可以通过采取member-variablesmember-methods虽然可以课程的混合搭配。

class Test {
    static Object CLASS_LOCK = new Object();
    Object MEMBER_LOCK = new Object();
    Test sharedTest = new Test(); // or created in different manner

    static void methodA() {
        synchronized (CLASS_LOCK) {
            // .. thread-safe code
        }

        synchronized (sharedTest.MEMBER_LOCK) {
            // .. thread-safe code
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM