简体   繁体   English

可以子类初始化超类变量

[英]can subclass initialize superclass variable

i have an activity say A that is extended by many other activities , i have a variable in activity A which is used by all other sub activities(classes) extending it.我有一个活动说 A 由许多其他活动扩展,我在活动 A 中有一个变量,它被所有其他扩展它的子活动(类)使用。 I want to know if i can set a value for that variable from a sub-activity such that the change will reflect in all the other subclasses extending Activity A. i know its a basic question ,i am new to this any help is appreciated.我想知道我是否可以从子活动中为该变量设置一个值,以便更改将反映在扩展活动 A 的所有其他子类中。我知道这是一个基本问题,我是新手,任何帮助表示赞赏。

eg:
Activity A has
public String global = "ABC"

Activity B extent A
display(global); ---> ABC

Activity C extent A
display(global); ---> ABC

Activity D extent A
display(global); ---> ABC  

now how can i change global in Activity D such that Activity B and C should also be affected.现在我如何在活动 D 中更改全局,以便活动 B 和 C 也应该受到影响。

Seems like you want a variable whos value persists and remains same throughout.似乎您想要一个变量,其值始终存在并且始终保持不变。

But since you only want to your inherited classes to be able to update or read the variable, you can do something like this:但是由于您只希望继承的类能够更新或读取变量,您可以执行以下操作:

class A
{
  private static int your_var = 0;

  public int get()
  {
     return your_var;
  }
  public void set(int a)
  {
      your_var = a;
  }
}

 class B extends A
  {

  }

  class C extends A
  {

  }

In your static void main:在你的静态无效主要:

  new B().set(101);

  new C().get()  // this will return value 101.

Note: Here only one copy of variable your_var is created.注意:这里只创建了一个变量 your_var 的副本。 And since it is private, only the non static getter setter methods will be able to read or modify it.由于它是私有的,因此只有非静态 getter setter 方法才能读取或修改它。 Thus making it accessible only by either the containing class itself or the child class objects.从而使其只能由包含类本身或子类对象访问。

You need to create a static variable in Activity A.您需要在活动 A 中创建一个静态变量。

A static variable is a variable of the class and not of the objects.静态变量是类的变量,而不是对象的变量。

try the following: in class A尝试以下操作:在 A 类中

public static String global = "ABC";

in class B B级

public class B extends A {


    public static void main(String[] args) {
    B b = new B();
    A a = new A();
    System.out.println(b.global);
    System.out.println(a.global);
    System.out.println(global);
    }

}

alternatively you could try to work with encapsulation: mark the string as private and make the "global" string available through getters and setters.或者,您可以尝试使用封装:将字符串标记为私有,并通过 getter 和 setter 使“全局”字符串可用。 This is often a more flexible solution then working with static variables这通常是一个比使用静态变量更灵活的解决方案

http://www.tutorialspoint.com/java/java_encapsulation.htm http://www.tutorialspoint.com/java/java_encapsulation.htm

You can have a singleton class which can hold global data and when need you can fetch the data from the single commonly shared instance of the singleton class.您可以拥有一个可以保存全局数据的单例类,并且在需要时您可以从单例类的单个公共共享实例中获取数据。 But there is another better way.但还有另一种更好的方法。

You need a class which sits on top of your activities.您需要一个位于您的活动之上的课程。 In any app we usually do have one such class (may be some Initilizer or main or manager class ) which wires the entities and initiates our application.在任何应用程序中,我们通常都有一个这样的类(可能是一些初始化程序或主或管理器类),它连接实体并启动我们的应用程序。

In android we have Application class which can hold global data.在android中,我们有可以保存全局数据的Application类。 For reference see Android global variable参考Android 全局变量

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

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