是否有来自不同类的引用的动态对象类型?
好的,我有两个课程,第一个课程名为 Class1,第二个课程名为 Class2。 所以我想做的是将 Class1 的引用发送到 Class2 中。为此,我需要以下类型的代码:
Class1:
public class Class1 extends Activity {
/** Called when the activity is first created. */
public int Var1 = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Class2(this));
}
}
public class Class2 extends SurfaceView {
Class1 Obj1; //Here!
public Class2(Class1 Obj){ //And here I don't want to
//Use Class1 as a type.
//Is there any Global type?
this.Obj1 = Obj;
}
public void EditVar1(){
Obj.Var1 = 10;
}
}
要获取 Class1 的引用:我应该使用对象的类型作为 Class1。我想在多个项目中使用这些代码:所以我应该为每个项目编辑 Obj 的类型“Class1”,只要它们的主类名称不同。这里有可以使用的全局类型吗?我还尝试使用 Context 作为对象类型,但我无法到达 Var1。反正我也不知道是什么。如果有人能解释它是什么,我将不胜感激。感谢您的任何回复!
Ok I have two classes the first one is named Class1 and the second one is Class2.
So the thing I want to do is sending a reference of Class1 into the Class2. To do this I need these kind of codes:
Class1:
public class Class1 extends Activity {
/** Called when the activity is first created. */
public int Var1 = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Class2(this));
}
}
public class Class2 extends SurfaceView {
Class1 Obj1; //Here!
public Class2(Class1 Obj){ //And here I don't want to
//Use Class1 as a type.
//Is there any Global type?
this.Obj1 = Obj;
}
public void EditVar1(){
Obj.Var1 = 10;
}
}
To take a reference of Class1: I should use Object's type as Class1. I want to use these codes in more than one projects: So I should edit the Obj's type "Class1" for each project as long as their main class name is different. Is there any global type to use here? Also I tried to use Context as object type with this I can't reach Var1. Anyway I don't know what is it too. If someone can explain what is it I'll be appreciative. Thank you for any responds!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,它们最近的祖先是 java.lang.Object,就像所有 Java 类一样。
对于 Activity,它的父级是上下文,正如您尝试使用的那样。如果您想访问 Activity 具有的父 Activity 没有的特定属性和方法,则必须强制转换该对象。例如:
如果保留对超类而不是派生类的引用,并将派生对象分配给超类对象,则会丢失派生类的特定属性。这称为对象切片。
Their nearest ancestor in this case is java.lang.Object, like all Java classes.
For Activity, its parent is context, as you tried using. If you then want to access the specific properties and methods an Activity has, that its parent doesn't have, you have to cast the object. E.g.:
If you keep a reference to the superclass instead of the derived class and you assign the derived object to the superclass object, you lose the specific properties of the derived. This is known as object slicing.