父类为子类定义静态类

omqzjyyz  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(269)

想知道是否有一种方法可以在java中做到这一点。
假设我想创建一个父类(可能是抽象的)并创建多个不同的子类。我希望所有类都有一个特定的字段,但我需要该字段在子上下文中是静态的。有没有办法在父类中定义这个?
例如,假设我对名为foo的字段有getter和setter。在父类中,静态字段被初始化为字符串“foo”。我有两个儿童班abschildone和ABSCHILDON2。我想做的是:

AbsChildOne absOneA = new AbsChildOne();
    AbsChildOne absOneB = new AbsChildOne();
    AbsChildTwo absTwo = new AbsChildTwo();

    absOneA.setFoo("bar");
    System.out.println("absOneB "+absOneB.getFoo());
    System.out.println("absTwo "+absTwo.getFoo());

返回

absOne2 bar
absTwo foo

换句话说,静态字段对于子类来说只是静态的,而不是其他子类。

ax6ht2ek

ax6ht2ek1#

事实是 static 在类级别,因此每次都有一个值 classes 包括 subclasses (不要与类示例混淆)。因此,在使用static(甚至init)时,不需要有示例( new ) String s_get_static_value = ClassName.value . 每次,每次 ClassName_instances 一次只有一个值适用于所有情况 instances . 因此,一旦更改(在类级别,instance)将反映到所有示例(隐含的类级别)。
关键是不能保持从同一个静态派生的2个单独的静态(总是有1个值而不是2)。
现在,如果希望有一个扩展字段父级,每个子级有一个单独的值(类级只有1个,所有示例都派生自 Type . Type 也可以是一个不同的子类。 Parent_Type 不等于 Child_type 即使 Child 延伸 Parent ).
考虑逻辑的偶然性。(要求:与你的相同,但没有静态)

//in term of field_value ; all in the same time and just 1 based on Types
parent = A (on Parent class) : Type Parent
parent = A1 (on Child1 at inhereted field from Parent) : Type Child1 
parent = A2 (on Child2 at inhereted field from Parent) : Type Child2

//oneInstance is doing that task which is an object based on Singleton
//can hold as may parameters as wanted (mystring in this case)

也许这能帮你。

//adjusted singleton with a map which hold instances
import java.util.HashMap;

import tst.Test02.Child1;
import tst.Test02.Child2;

public class OneInstance 
{
    String myString = "";
    private OneInstance oi ;
    public static HashMap<String, OneInstance> hm = new HashMap<String,OneInstance>();
    public static OneInstance getInstance(Object o)
    {
        if(!hm.containsKey(o.getClass().toString()))
            hm.put(o.getClass().toString(), new OneInstance("default value for _"+o.getClass().toString()));
        return hm.get(o.getClass().toString());

    }
    public static OneInstance getInstance(Object o,String s)
    {
        if(!hm.containsKey(o.getClass().toString()))
            hm.put(o.getClass().toString(), new OneInstance(s));
        return hm.get(o.getClass().toString());

    }
    public static OneInstance getInstance()
    {
        if(!hm.containsKey(new Parent().getClass().toString()))
            hm.put(new Parent().getClass().toString(), new OneInstance("default value for Parent"));
        return hm.get(new Parent().getClass().toString());
    }

    private OneInstance() {}
    private OneInstance(String s)
    {

        myString = s;
    }

}
public class Parent 
{
    static MyObj sobj = new MyObj("static name");
    OneInstance oi;

    public String toString()
    {
        return "myString="+oi.getInstance().myString;
    }
}
//usage
import java.util.Set;

public class Test02 
{
    public static void main(String[] args)
    {
        Test02 t = new Test02();
        Child1 c1;
        Child2 c2;
        System.out.println("inhereted name for child1:"+Child1.sobj.name);
        Child1.sobj.name = "child1_new_name";
        System.out.println("child1 new name:"+Child1.sobj.name);
        System.out.println("inhereted name for child2:"+Child2.sobj.name);
        System.out.println("-----------");
        System.out.println(new Parent());
        c1 = t.new Child1("child1_string");

        //with any specification just init with parent value and keep
        System.out.println(c1);
        System.out.println(c1.oi.getInstance(c1).myString);
        System.out.println("-----------");

        System.out.println("dump HashMap");
        OneInstance.hm.forEach((k, v) -> System.out.println((k + ":" + v.myString)));
        System.out.println("-----------");
        c2 = t.new Child2("child2_string from constructor"); 
        c1.oi.getInstance(c1).myString = "changed for child 1";
        System.out.println(c1.oi.getInstance(c1).myString);
        System.out.println("dump HashMap");
        OneInstance.hm.forEach((k, v) -> System.out.println((k + ":" + v.myString)));
        System.out.println("-----------");
        c2 = t.new Child2("child2_string from constructor"); 
        System.out.println(c2);

        //System.out.println(c2.oi.getInstance(c2).myString);
        //c2.oi.getInstance(c2).myString = "changed for child 2";
        //System.out.println(c2.oi.getInstance(c2).myString);

        System.out.println(c1);
        System.out.println(c1.oi.getInstance(c1).myString);
        System.out.println(c2.oi.getInstance(c2).myString);
        System.out.println("dump HashMap");
        OneInstance.hm.forEach((k, v) -> System.out.println((k + ":" + v.myString)));
        System.out.println("-----------");

    }

    class Child1 extends Parent
    {
         //inherited
         //OneInstance oi ;
         Child1(String s)
         {
             oi = OneInstance.getInstance(this, s);
         }
         public String toString()
         {
            return "myString="+oi.getInstance(this).myString;
         }
    }

    class Child2 extends Parent
    {
         //OneInstance oi ;
         Child2(String s)
         {
             oi =OneInstance.getInstance(this, s);
         }
         public String toString()
         {
            return "myString="+oi.getInstance(this).myString;
         }
    }
}

输出:

inhereted name for child1:static name
child1 new name:child1_new_name
inhereted name for child2:child1_new_name
-----------
myString=default value for Parent
myString=child1_string
child1_string
-----------
dump HashMap
class tst.Test02$Child1:child1_string
class tst.Parent:default value for Parent
-----------
changed for child 1
dump HashMap
class tst.Test02$Child1:changed for child 1
class tst.Test02$Child2:child2_string from constructor
class tst.Parent:default value for Parent
-----------
myString=child2_string from constructor
myString=changed for child 1
changed for child 1
child2_string from constructor
dump HashMap
class tst.Test02$Child1:changed for child 1
class tst.Test02$Child2:child2_string from constructor
class tst.Parent:default value for Parent
-----------

关于测试,在代码开发之前。如果代码中存在逻辑错误,那么测试会带来什么结果呢(整个测试套件将失败,并且在继续之前有一些关键缺陷需要解决……)很好地了解测试api的各种设置\ujunit,但这不是主要范围。代码设计是为了正常运行,而不是为了测试而进行的代码设计(主要问题来自代码设计 static 而不是来自 how to test )
注意:您必须添加一些逻辑来根据继承进行调整(保留字段),但是为什么不更改逻辑并允许另一个不同的字段作为子字段呢?它很容易实现,也可以使用静态的。

class Parent 
{
   static String parent;
}
class Child1 extends Parent 
{
   //inhereted
   //static String parent;

   //add a new one per subclasses
   static String child1;
}

只需比较每个场景所需的代码行,并询问它是否真的值得?度量(即使在代码行内)也在质量代码中起作用。

相关问题