我想知道将私有方法声明为最终是否有意义,我认为这没有意义.但我想象有一个独特的情况,并编写代码来弄清楚:
public class Boom {
private void touchMe() {
System.out.println("super::I am not overridable!");
}
private class Inner extends Boom {
private void touchMe() {
super.touchMe();
System.out.println("sub::You suck! I overrided you!");
}
}
public static void main(String... args) {
Boom boom = new Boom();
Boom.Inner inner = boom.new Inner();
inner.touchMe();
}
}
Run Code Online (Sandbox Code Playgroud)
它编译和工作."我应该让touchMe()最终成功"我想到并做到了:
public class Boom {
private final void touchMe() {
System.out.println("super::I am not overridable!");
}
private class Inner extends Boom {
private void touchMe() {
super.touchMe();
System.out.println("sub::You suck! I …Run Code Online (Sandbox Code Playgroud) 两个关键字Shadows和Overrides有什么意义?他们做了什么以及哪个环境是一个或另一个更好?
让我们假设Visual C++ 2010中的这种情况:
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
public:
int b;
void Display()
{
cout<<"Base: Non-virtual display."<<endl;
};
virtual void vDisplay()
{
cout<<"Base: Virtual display."<<endl;
};
};
class Derived : public Base
{
public:
int d;
void Display()
{
cout<<"Derived: Non-virtual display."<<endl;
};
virtual void vDisplay()
{
cout<<"Derived: Virtual display."<<endl;
};
};
int main()
{
Base ba;
Derived de;
ba.Display();
ba.vDisplay();
de.Display();
de.vDisplay();
_getch();
return 0;
};
Run Code Online (Sandbox Code Playgroud)
从理论上讲,这个小应用程序的输出应该是:
因为Base类的Display方法不是虚方法,所以Derived类不能覆盖它.对?
问题是,当我运行应用程序时,它会打印出:
我有一个父类Parent和一个子类Child,由此定义:
class Parent {
@MyAnnotation("hello")
void foo() {
// implementation irrelevant
}
}
class Child {
@Override
foo() {
// implementation irrelevant
}
}
Run Code Online (Sandbox Code Playgroud)
如果我获得Method参考Child::foo,childFoo.getAnnotation(MyAnnotation.class)会给我@MyAnnotation吗?还是会的null?
我更感兴趣的是注释如何或是否与Java继承一起使用.
是否有关于如何重写特定规则equals()与hashCode()在子类考虑超领域?知道有很多参数:超级字段是私有/公共的,有/无getter ...
例如,Netbeans生成的equals()&hashCode()将不考虑超级字段...和
new HomoSapiens("M", "80", "1.80", "Cammeron", "VeryHot").equals(
new HomoSapiens("F", "50", "1.50", "Cammeron", "VeryHot"))
Run Code Online (Sandbox Code Playgroud)
将返回true :(
public class Hominidae {
public String gender;
public String weight;
public String height;
public Hominidae(String gender, String weight, String height) {
this.gender = gender;
this.weight = weight;
this.height = height;
}
...
}
public class HomoSapiens extends Hominidae {
public String name;
public String faceBookNickname;
public HomoSapiens(String gender, String weight, String height,
String name, String …Run Code Online (Sandbox Code Playgroud) 我已经创建了自己的主题作为单独的Maven项目,并且它已正确加载.
现在我想改变组件的大小.例如,a <p:orderList>.它有一个名为的类ui-orderlist-list,它primefaces.css以固定的200x200维度定义.无论我在我身上做什么theme.css,它都会被这个属性所覆盖,我无法将内容作为<p:orderList>更广泛的内容.
对于其他组件,我可能只想覆盖组件的一个实例,而不是全部.
任何人都可以告诉我,我怎么能做到这一切?
我正在研究在JAVA中重写成员函数,并考虑尝试重写成员变量.
所以,我定义了类
public class A{
public int intVal = 1;
public void identifyClass()
{
System.out.println("I am class A");
}
}
public class B extends A
{
public int intVal = 2;
public void identifyClass()
{
System.out.println("I am class B");
}
}
public class mainClass
{
public static void main(String [] args)
{
A a = new A();
B b = new B();
A aRef;
aRef = a;
System.out.println(aRef.intVal);
aRef.identifyClass();
aRef = b;
System.out.println(aRef.intVal);
aRef.identifyClass();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
1
I am …Run Code Online (Sandbox Code Playgroud) @Override除了让编译器检查超类是否具有该方法之外,是否有任何理由使用其他方法来注释方法?
我想知道:
overriding ×10
java ×6
inheritance ×2
.net ×1
annotations ×1
c++ ×1
css ×1
equals ×1
final ×1
hashcode ×1
jsf ×1
oop ×1
operators ×1
overloading ×1
primefaces ×1
python ×1
shadows ×1
static ×1
themes ×1
vb.net ×1