事实上,static 是类级别的,因此每个 classes 包括 subclasses 每次都会有一个值(不要与类实例混淆)。所以在使用静态(甚至初始化)时不需要有实例。(new)
String s_get_static_value = ClassName.value。对于每个ClassName_instances,每次只有一个值适用于所有instances。所以一旦改变(在类级别,实例)将反映到所有实例(隐含类级别)。
关键是你不能保留从同一个静态派生的 2 个单独的静态(总是有 1 个值而不是 2)。
现在,如果想要有一个扩展字段父级,每个子级具有单独的值(并且在类级别只有 1 个,并且所有实例都派生自 Type。Type 也可以是一个不同的子类。@987654330 @ 与 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_junit 的各种设置,但这不是主要范围。代码设计可以正常运行,而不是用于测试目的的代码设计。
(主要问题来自代码设计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;
}
只需比较每个场景所需的代码行,然后询问是否真的值得?指标(甚至在代码行内)也计入质量代码。