多态问题 不太明白你的意思你的Child现在是怎么设计的?多态体现在成员方法上好像不会体现在成员属性上的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 创建child2 的时候 child2.field 会把child1.field冲掉。但child1.field又是经常使用的。 创建child2 的时候 child2.field 会把child1.field冲掉为什么会这样?静态属性只在第一次载入类的时候初始化创建实例的时候不会再去动它如果你的类有这个问题应该是你的构造函数设计上出的问题。 package test;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class Parent { public static String field[] = null; public Parent() { } public void print() { for( int i=0;i<field.length;i++) { System.out.println(field[i]); } } public static void main(String[] args) { Parent parent1 = new Child1(); parent1.print(); Parent parent2 = new Child2(); parent1.print(); }}class Child1 extends Parent{ public Child1() { field =new String[]{"1","2"}; }}class Child2 extends Parent{ public Child2() { field =new String[]{"3","4"}; }} 你的代码每次创建一个新的Child都会对Parent类的field赋一个新的String的对象,如果希望Child1和Child2互不影响的话,定义他们自己的field就行了 一些操作想写到父类用到 但子类的field属性又应该是静态的。 这样可以吗?:package test;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public abstract class Parent { public Parent() { } public abstract void print();}class Child1 extends Parent{ public static String field[] = null; public Child1() { field =new String[]{"1","2"}; } public void print() { for( int i=0;i<field.length;i++) { System.out.println(field[i]); } }}class Child2 extends Parent{ public static String field[] = null; public Child2() { field =new String[]{"3","4"}; } public void print() { for( int i=0;i<field.length;i++) { System.out.println(field[i]); } }}public class Test { public static void main(String[] args) { Parent parent1 = new Child1(); parent1.print(); Parent parent2 = new Child2(); parent1.print(); }}我没有编译测试,不保证正确,但就是这个意思 ^_^ parent 无法调用field了很多操作都需要field 例如getFieldCount而且同样的操作还得在每个子类里再写一边 把父类写成抽象类我觉得既然你的field属性是具有子类特征的就应该把field属性放入子类去实现 就像是 数据表操作 表有一些通用的操作,但每个表又都有自己的field这些field 应当是不变的。是不是应该通过设计模式来改变一下实现方式 静态方法和属性不支持多态!!!!--------------------------------------------------nod、nod ^_^ package test;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class Parent { protected String field[] = null; public Parent() { } public void print() { for( int i=0;i<this.field.length;i++) { System.out.println(field[i]); } } public static void main(String[] args) { Parent parent1 = new Child1(); parent1.print(); Parent parent2 = new Child2(); parent1.print(); }}class Child1 extends Parent{ public Child1() { this.field =new String[]{"1","2"}; }}class Child2 extends Parent{ public Child2() { this.field =new String[]{"3","4"}; }} 楼上将field改为成员属性,这样当然没有问题不过楼主想要的静态的类属性... 请教一个ResponseEntity 的问题 请高手帮我分析一段程序 先谢谢了! C盘少2个G,帮帮忙 GUI程序与WEB程序的集成问题,jsp页面链接 如何实现JTextField鼠标双击事件,急救!!! 为什么我的rmi 类文件编译通过以后 执行rmic命令 报 找不到类的错误呢 正则表达该怎么写,100分 客户端和服务器段不断传输数据流的问题 能不能帮我翻译一段句子。看过的就知道。 300分求教解决方案(即将加分) CLASSPATH,设置了直接使用JAVA能运行,但打成JAR包就报找不到所需要的类,WHY 为什么我的java程序不能运行?
静态属性只在第一次载入类的时候初始化
创建实例的时候不会再去动它
如果你的类有这个问题
应该是你的构造函数设计上出的问题。
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Parent {
public static String field[] = null;
public Parent() {
}
public void print()
{
for( int i=0;i<field.length;i++)
{
System.out.println(field[i]);
}
}
public static void main(String[] args) { Parent parent1 = new Child1();
parent1.print();
Parent parent2 = new Child2();
parent1.print(); }}
class Child1 extends Parent
{
public Child1()
{
field =new String[]{"1","2"};
}
}
class Child2 extends Parent
{
public Child2()
{
field =new String[]{"3","4"};
}
}
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public abstract class Parent {
public Parent() {
}
public abstract void print();
}class Child1 extends Parent
{
public static String field[] = null;
public Child1()
{
field =new String[]{"1","2"};
}
public void print()
{
for( int i=0;i<field.length;i++)
{
System.out.println(field[i]);
}
}
}class Child2 extends Parent
{
public static String field[] = null;
public Child2()
{
field =new String[]{"3","4"};
}
public void print()
{
for( int i=0;i<field.length;i++)
{
System.out.println(field[i]);
}
}
}public class Test {
public static void main(String[] args) {
Parent parent1 = new Child1();
parent1.print();
Parent parent2 = new Child2();
parent1.print();
}
}我没有编译测试,不保证正确,但就是这个意思 ^_^
而且同样的操作还得在每个子类里再写一边
就应该把field属性放入子类去实现
是不是应该通过设计模式来改变一下实现方式
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Parent {
protected String field[] = null;
public Parent() {
}
public void print()
{
for( int i=0;i<this.field.length;i++)
{
System.out.println(field[i]);
}
}
public static void main(String[] args) { Parent parent1 = new Child1();
parent1.print();
Parent parent2 = new Child2();
parent1.print(); }}
class Child1 extends Parent
{
public Child1()
{
this.field =new String[]{"1","2"};
}
}
class Child2 extends Parent
{
public Child2()
{
this.field =new String[]{"3","4"};
}
}
不过楼主想要的静态的类属性...