- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下代码。以下是两个问题:
class Parent {
private void test()
{
System.out.print("test executed!");
}
static void print(){
System.out.println("my class is parent");
}
}
class Child extends Parent
{
static void print(){
System.out.println("my class is Child");
}
}
public class Inheritence {
public static void main(String[] args)
{
Child p1 = new Child();
Parent p = new Child();
System.out.println("The class name of p is "+p.getClass());
System.out.println("The class name of p1 is "+p1.getClass());
System.out.println("p instance of child "+ (p instanceof Child));
System.out.println("p1 instance of child "+ (p1 instanceof Child));
//p.test();
p.print();
}
}
The class name of p is class Child
The class name of p1 is class Child
p instance of child true
p1 instance of child true
my class is parent
p
的类名将是
Parent
,因为它的类型是
Parent
。但是,它打印为
Child
。所以我怎么得到
type
的
p
。
class Child1 extends Parent1
{
}
public class Parent1 {
private void test()
{
System.out.print("test executed!");
}
public static void main(String[] args)
{
Parent1 p = new Child1();
p.test();
Child1 c = new Child1();
//c.test(); The method test from parent1 is not visible
}
}
Output is : test executed!
test
类型为
Child1
的对象上调用
Parent1
方法。
Child1
没有
test
方法,因为它没有被继承。但是我仍然得到输出,表明私有方法是继承的!如果
test
是受保护的方法,并且我在子类中重写,则尽管被调用的对象类型是parent
(parent p1 = new child1());
,它还是会被执行的重写方法
p.test
。
class Child1 extends Parent1
{
}
class Parent1 {
private void test()
{
System.out.print("test executed!");
} }
public class App1{
public static void main(String[] args)
{
Parent1 p = new Child1();
p.test();//The method test from parent is not visible
Child1 c = new Child1();
//c.test(); //The method test from parent1 is not visible
}
}
最佳答案
解决第二个问题:保持“继承”和“可见性”的概念可能会有所帮助。 private
方法m
仅在声明该方法的类C
内部可见。因此,您完全不能从m
外部使用C
。此外,即使在C
内部,也不能使用x.m()
,除非将对象x
声明为C
类型。但是子类的对象仍然具有方法m
,可以调用该方法,但只能在C
内部。此示例将进行编译(即使两个类位于单独的文件中):
public class Class1 {
private void test () { System.out.println ("test"); }
public void doThis (Class2 c) {
// c.test(); -- does not compile [1]
((Class1)c).test();
}
}
public class Class2 extends Class1 {
public void doSomething () {
doThis (this);
// ((Class1)this).test(); -- does not compile [2]
}
}
doThis
中,即使
test
的类型为
c
,您仍然可以调用
c
的
Class2
方法。但是,您只能这样做是因为代码在声明
Class1
方法的类
test()
中(这就是[2]不编译的原因);并且只能通过将
c
强制转换为
Class1
类型的对象(这就是[1]无法编译的原因)来实现。但是,即使在强制转换它以使编译器将其视为类型为
Class1
的表达式之后,实际对象仍然具有类型为
Class2
的对象。如果要像这样调用覆盖的方法
polymorphic
:
((Class1)c).polymorphic()
,它将调用在
Class2
中定义的方法,而不是在
Class1
中定义的方法,因为该对象实际上仍然是
Class2
对象。
test
是继承的,即使它是私有的。它只是在
Class2
中不可见。
Parent x;
,则
x
的编译时类型为
Parent
。如果您的函数
f
的返回类型为
Parent
,则类似
obj.f(arg,arg2)
的表达式的类型将为
Parent
。但是在运行时,如果变量或表达式的编译时类型为
Parent
,则运行时的实际类型可以为
Parent
或其任何子类。运行时类型将基于对象的构造方式。因此,变量可以具有编译时类型
Parent
和运行时类型
Child
。然后,您只需要知道使用哪种类型以及何时使用。为了检查方法是否可见(在其中使用
private
),使用了编译时类型。为了确定子类重写方法时要调用的方法,使用了运行时类型(这就是多态性)。对于
.getClass()
,使用运行时类型。无论如何,我就是这样考虑事情的,这样我就不会太困惑。
class Parent { }
class Child extends Parent { }
class Grandchild extens Child { }
Parent x1 = new Parent();
Parent x2 = new Child();
Parent x3 = new Grandchild();
x1
,
x2
和
x3
的编译时类型均为
Parent
。这意味着所有三个变量都可以引用一个实例
Parent
或
Child
或
Grandchild
或任何其他实例的
Parent
的子类。上面就是这样:
x2
将引用
Child
的实例,而
x3
将引用的实例
Grandchild
。
private Parent getAParent(int n) {
if (n == 0) return new Parent();
if (n == 1) return new Child();
if (n == 2) return new Grandchild();
throw new IllegalArgumentException();
}
Parent x4 = getAParent (0);
Parent x5 = getAParent (1);
Parent x6 = getAParent (2);
x5
引用
Child
的实例,
x6
引用一个实例
Grandchild
。
getAParent
的编译时类型
Parent
。编译器不知道哪个类
play()
中声明方法
Grandchild
,则它们是
x3.play (); // ERROR
x6.play (); // ERROR
getAParent(2).play (); // ERROR
getAParent(2)
Parent
,而不是
Grandchild
。并且
play
没有定义
Parent
。要查看这些变量是否具有运行时类型,
play
方法将要求编译器生成代码以
p.test()
在第二个示例中起作用的原因。即使
p
Child1
的实例,
p
的编译时类型为
Parent1
,因为您已将其声明为
Parent1
类型。和
Parent1
具有
test
方法;由于代码
Parent1
内部,即使
test
方法是可见的
p
实际引用的运行时类型。希望这可以帮助
关于java - 如何获取Java中引用的变量类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18776099/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!