如何在java中复制类
我的项目由两个类组成:类型1和类型2,每个类都有自己的函数和字段 我将按如下方式使用它们:如何在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
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可以是:
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都是一个变量,只能调用Supertype或ittype中的方法。如果您需要类型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中的“对象”类型,不要使用