如何在java中复制类

如何在java中复制类,java,android,Java,Android,我的项目由两个类组成:类型1和类型2,每个类都有自己的函数和字段 我将按如下方式使用它们: private void initialize(String type) { if (type == "Type1") { x = new Type1; } else if (type == "Type2") { x = new Type2; } } X变量必须是什么类型的 ============================= public cl

我的项目由两个类组成:类型1和类型2,每个类都有自己的函数和字段 我将按如下方式使用它们:

private void initialize(String type) {
    if (type == "Type1") {
      x = new Type1;
    } else if (type == "Type2") {
      x = new Type2;
    }
}
X变量必须是什么类型的

=============================

  public class Type1 extends SuperClass{
    public int var = 1;
  }

  public class Type2 extends SuperClass{
    public int var = 2;
  }

  private void initialize(String type) {
    switch (type) {
      case "Type1":
        x = new Type1();
        break;
      case "Type2":
        x = new Type2();
        break;
    }
  }

  void main(){
    //int num = x.var;
  }
我使用超类和接口,但我不能访问类型1或类型2的变量和方法,只能访问超类的变量和方法

=============================

  public class Type1 extends SuperClass{
    public int var = 1;
  }

  public class Type2 extends SuperClass{
    public int var = 2;
  }

  private void initialize(String type) {
    switch (type) {
      case "Type1":
        x = new Type1();
        break;
      case "Type2":
        x = new Type2();
        break;
    }
  }

  void main(){
    //int num = x.var;
  }

在这种情况下不能用于强制转换:
((Type1)x).var

如果希望
x
接受
Type1
Type2
的实例,则必须是它们共享的某个共同祖先或接口。如果尚未声明此类类型,则始终可以使用
对象
,它是所有类的共同祖先。

如果x是一个可以同时保存Type1和Type2的变量,则x可以是:

  • 两者的超类
  • 两个类都在实现的接口
  • 范例

    案例1 那你就可以了

    private void initialize(String type) {
        SuperType x = new Type1();
          //or
        SuperType x = new Type2();
    
      both are valid 
    
    private void initialize(String type) {
        IType x = new Type1();
          //or
        IType x = new Type2();
    
    案例2 那你就可以了

    private void initialize(String type) {
        SuperType x = new Type1();
          //or
        SuperType x = new Type2();
    
      both are valid 
    
    private void initialize(String type) {
        IType x = new Type1();
          //or
        IType x = new Type2();
    
    编辑 在这两种情况下,1和2 x都是一个变量,只能调用Supertypeittype中的方法。如果您需要类型1特定的方法,则需要强制转换

    ((Type1)x).myType1OnlyMethod ();
    

    如果要定义新对象,它必须是:

    Type1 x = new Type1;
    Type2 y = new Type2;
    
    然后这两个类中的构造函数将创建这个新对象

    如果你问的是继承的问题,那么答案也差不多。您希望拥有您的超类(提供子类将拥有的一些基线变量的类),然后您希望将其扩展到两种类型。大致上,它看起来是这样的

    public class Type{
        private String exampleVariable;
        ...
    }
    
    public class Type1 extends Type{
        ...
    }
    
    public class Type2 extends Type{
        ...
    }
    
    然后你可以通过

    Type a = new Type1();
    Type b = new Type2();
    
    像这样做

        If (Type instanceOf Type1) {
                                    x = new Type1;
                                  } 
       else if (Type instanceOf Type2)   {
                                    x = new Type2;
                                   }
    

    使用将由两个类实现的接口

    public interface Typeimplemntor{}
    public class Type1 implements Typeimplemntor{}
    public class Type2 implements Typeimplemntor{}
    
    之后,在你的职能

     private void initialize(String type) {
    Typeimplemntor typImp;
            if (type == "Type1") {
              typImp = new Type1();
            } else if (type == "Type2") {
              typImp = new Type2();
            }
          }
    
    更新的完整示例

        public class Test {
            public static void main (String argd[])
            {
                String type= "Type2";
                Typeimplemntor typeimplemntor = null;
                if (type.equals("Type1")) {
                    typeimplemntor = new Type1();
                }else if (type.equals("Type2")) {
                    typeimplemntor = new Type2();
                }
    
                typeimplemntor.print();
                if (typeimplemntor instanceof Type1) {
                    int y = ((Type1)typeimplemntor).x;
                    System.out.println(y);
                    ((Type1)typeimplemntor).notInInterfaceType1();
                }else if (typeimplemntor instanceof Type2){
                    int y = ((Type2)typeimplemntor).x;
                    System.out.println(y);
                    ((Type2)typeimplemntor).notInInterfaceType2();
                }
    
    
            }
        }
    
    public class Type1 implements Typeimplemntor {
        int x = 5;
        public void print () {
            System.out.println("Printed from Type1");
        }
       public void notInInterfaceType1 () {
        System.out.println("Not in interface but in Type1");
       }
    }
    
    public class Type2 implements Typeimplemntor {
        int x = 15;
        public void print () {
            System.out.println("Printed from Type2");
        }
        public void notInInterfaceType2 () {
           System.out.println("Not in interface but in Type2");
        }
    }
    
    public interface Typeimplemntor {
        void print();
    }
    

    如果您有将在这两个类中使用的方法,您可以在接口中定义它们,并且可以根据它们在类中的实现直接访问它们。如果还有其他方法或变量,那么您必须检查实例,然后将其转换为相同的方法或变量。

    只要您能够控制修改和写入
    Type1
    Type2
    ,创建通用接口就可以了

    但是如果您使用的是无法控制源代码的外部类,我建议使用
    对象
    引用


    无论如何,请确保您了解类中可用的所有方法,并在适用的情况下添加适当的类型安全/铸造功能

    你可以把X看作是java中的“对象”类型,不要使用==/COD>进行字符串比较,使X接口看起来像工厂模式的int创意设计模式,为什么你使用类,使用接口,进一步查看工厂模式,如果你不想铸造,为什么使用动态X,直接使用type1或type2的对象,除非我误解了这个问题,我相信这就是OP的全部要求?我使用这种方式,但我不能访问type1或type2的变量和方法,只能访问定义接口的超类(type)的变量和方法肯定是正确的方式!(有关更深入的知识,请参阅工厂模式或其他创建模式)这两种方法都是正确的,但考虑到OO原则,您应该更喜欢接口而不是继承…我使用这种方法(超类和接口)但我不能访问类型1或类型2的变量和方法,只能访问超类(类型)A的变量和方法。。。很简单。。。您需要将变量x强制转换为所需的类型类。。我将更新答案主要的问题是,应该强制转换除了强制转换之外没有其他方法?这样就需要进行强制转换,如果我不知道要将哪些类放入x中,那么如何强制转换变量x?x的值取决于字符串类型以这种方式转换的需要,我不知道要将哪些类放入x中,那么如何转换变量x?x的值取决于字符串类型