简体   繁体   English

如何在类中创建静态变量,只能由类修改,而不能由它的实例修改?

[英]How to make static variables inside a class, which can only be modified by the class only and not it's instances?

How to create a class variable which can only be modified by the class only and not it's instances如何创建一个只能由类而不是它的实例修改的类变量


class BullDog{
    String name;
    static String breed = "BullDog";

    public BullDog(String name){
        this.name = name;
    }
}

public class MainClass{
    public static void main(String [] args){
        Dog Mydog = new BullDog("Fluffy");
        Mydog.breed = "Shepherd"; // Should not be modified

        System.out.Println(Dog.breed);
    }
}

Real Output:实际输出:

Shepherd

Desired Output:期望输出:

BullDog

Make the field final that is constant.使字段final成为常量。 And static final is on class level. static final是在类级别。

public class BullDog {
    public String name;
    public static final String breed = "BullDog";

    public BullDog(String name){
        this.name = name;
    }
}

The problem is that inheritance is not possible.问题是继承是不可能的。 A Poodle needs its own static breed, and you cannot have a Dog providing a breed贵宾犬需要自己的静态品种,而您不能让狗提供品种

public abstract class Dog {
    public String name;

    public Dog(String name){
        this.name = name;
    }

    public abstract String getBreed();   
}

public class BullDog extends Dog {

    public BullDog(String name){
        super(name);
    }

    @Override
    public String getBreed() {
        return "Bulldog";
    }
}

public class Poodle extends Dog {

    public Poodle(String name){
        super(name);
    }

    @Override
    public String getBreed() {
        return "Wolf";
    }
}

List<Dog> dogs = Arrays.asList(new BullDog("fluffy"), new Poodle("Herman"));

Make the static variable private.将静态变量设为私有。 Refer to this for details.请参阅了解详情。

暂无
暂无

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

相关问题 如何制作仅在Boss类中使用的方法 - How to make method which can only be used if it is Boss class 有没有办法让 Class A 的变量只被特定接口的类修改? - Is there any way to make variables of Class A being only modified by classes of a particular interface only? 我如何使子 class 的字段和方法可用于 class,它仅通过另一个 class 使用超级 class - How can i make fields and methods of a sub class usable to a class which only uses the super class through another class 所有外部类实例总共只有1个静态嵌套类实例吗? 没有单例模式,只能出现1个实例吗? - Will there be only 1 static-nested class instance total for all the outer-class instances? Can only 1 instance occur without the singleton pattern? 需要制作只能由G实例化的A类 - Need to make a Class A which can be instaceated only from G 如何拥有只能由另一个特定 class 实例化的 class - How to have a class which can only be instantiated by another specific class 我们如何确保在任何时间仅调用类的静态方法和非静态方法之一? - How can we make sure that at any point of time only one of a static method & a non-static method of a class gets called? 我怎样才能创建一个只能在子类内部调用访问的方法? - How can i make a method with call access only inside the child class? 应该在仅实例化一次的类中使用静态变量 - Should static variables be used in a class that only gets instantiated once 仅带有“ private final static”变量的java类。这是一个好主意吗? - A java class only with “private final static” variables.. Is it a good idea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM