我遇到了一个有趣(而且非常令人沮丧)的问题,equals()今天的方法导致了我认为是经过良好测试的类崩溃并导致我花了很长时间才能追踪的错误.
为了完整起见,我没有使用IDE或调试器 - 只是老式的文本编辑器和System.out.时间非常有限,这是一个学校项目.
无论如何 -
我开发一个基本的购物车可能包含ArrayList的Book对象.为了贯彻落实addBook(),removeBook()以及hasBook()对车的方法,我想检查是否Book已在存在Cart.我走了 -
public boolean equals(Book b) {
... // More code here - null checks
if (b.getID() == this.getID()) return true;
else return false;
}
Run Code Online (Sandbox Code Playgroud)
一切都在测试中运行良好.我创建了6个对象并用数据填充它们.做了很多添加,删除,has()操作Cart,一切正常.我读到你可以拥有equals(TYPE var)或者equals(Object o) { (CAST) var }假设因为它有效,所以没关系太多.
然后我遇到了一个问题 - 我需要在Book类中创建一个只包含它的Book对象.不会输入任何其他数据.基本上如下:ID
public boolean hasBook(int i) {
Book b = new Book(i); …Run Code Online (Sandbox Code Playgroud) 我问的是一个非常简单的问题,但我对此感到困惑.
假设我有一个班级Parent:
public class Parent {
int name;
}
Run Code Online (Sandbox Code Playgroud)
并有另一堂课Child.java:
public class Child extends Parent{
int salary;
}
Run Code Online (Sandbox Code Playgroud)
最后是我的Main.java类
public class Main {
public static void main(String[] args)
{
Parent parent = new Child();
parent.name= "abcd";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我做一个像儿童的对象
Child child = new Child():
Run Code Online (Sandbox Code Playgroud)
然后child对象可以访问这两个name and salary变量.
我的问题是:
Parent parent = new Child();
Run Code Online (Sandbox Code Playgroud)
只name提供父类变量的访问权限.那么这条线的确切用途是什么?
Parent parent = new Child();
Run Code Online (Sandbox Code Playgroud)
而且当它使用动态多态时,为什么在执行此操作后无法访问子类的变量
Parent parent = new Child();
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,第一个和第二个打印语句如何打印出SubObj?顶部和子指向同一个Sub类吗?
class Top {
public String f(Object o) {return "Top";}
}
class Sub extends Top {
public String f(String s) {return "Sub";}
public String f(Object o) {return "SubObj";}
}
public class Test {
public static void main(String[] args) {
Sub sub = new Sub();
Top top = sub;
String str = "Something";
Object obj = str;
System.out.println(top.f(obj));
System.out.println(top.f(str));
System.out.println(sub.f(obj));
System.out.println(sub.f(str));
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码返回以下结果.
SubObj
SubObj
SubObj
Sub
Run Code Online (Sandbox Code Playgroud) 所以我有一类这样的重载方法:
class Foo {
public void test(Object value) {
...
}
public void test(String value) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我需要根据其类型将bean的属性值传递给其中一个方法,但在运行时之前我不知道实际的属性类型.例如
public void run(Object bean, String propertyName) {
Foo foo = new Foo();
foo.test(PropertyUtils.getProperty(bean, propertyName));
}
Run Code Online (Sandbox Code Playgroud)
BTW,PropertyUtils.getProperty()是一个辅助方法,它返回bean上指定属性的值.PropertyUtils.getProperty()返回一个Object,因此 test(Object value)将始终调用它,并忽略实际的属性类型.
我可以在运行时中找出propery类型,即使它的值为null.在Java中有动态转换这样的东西吗?如果没有,是否有一种方法可以使用正确的参数类型调用重载方法?
我有3个班GrandParent,Parent和Child,其中
Child extends Parent和Parent extends GrandParent
public class Main {
void test(GrandParent gp){System.out.println("GrandParent");}
void test(Parent p){System.out.println("Parent");}
public static void main(String args[]){
GrandParent obj = new Child();
Main mainObj = new Main();
mainObj.test(obj); // This calls test(GrandParent gp)
mainObj.test(new Child()); // This calss test(Parent gp)
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,2调用test()方法都用Child对象调用不同的方法.其中一个是编译时和其他运行时绑定.这对我来说听起来很奇怪.你会怎么解释这个?
鉴于Date有一个名为"after(Date)"的方法,而Timestamp有一个方法覆盖它称为"after(Timestamp)",为什么在下面的代码中调用Date中的after方法?
至于意外结果有人问在这里.
java.sql.Timestamp one = new java.sql.Timestamp(1266873627200L);
java.sql.Timestamp two = new java.sql.Timestamp(1266873627000L);
java.util.Date oneDate = (java.util.Date) one;
java.util.Date twoDate = (java.util.Date) two;
System.out.println("one: " + oneDate.getTime());
System.out.println("two: " + twoDate.getTime());
if (oneDate.after(twoDate)) {
System.out.println(oneDate.getTime() + " after " + twoDate.getTime());
} else {
System.out.println(oneDate.getTime() + " not after " + twoDate.getTime());
}
Run Code Online (Sandbox Code Playgroud)
结果
one: 1266873627200
two: 1266873627000
1266873627200 not after 1266873627000
Run Code Online (Sandbox Code Playgroud) 这是我的示例代码:
String str = "hello";
Object obj = (Object)str;
System.out.println(obj.toString());
Run Code Online (Sandbox Code Playgroud)
我找到了Object的源代码,toString()方法是:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Run Code Online (Sandbox Code Playgroud)
我认为这个例子的结果是这个Object的地址,比如[B @ 15db9742,在我将str转换为Object之后,但它仍然打印你好.为什么?Shound not obj使用Object的方法?任何人都可以向我解释它的原理吗?
我正在练习考试,发现了一个让我完全迷失的样本问题.对于以下代码,找到输出的内容:
class Moe {
public void print(Moe p) {
System.out.println("Moe 1\n");
}
}
class Larry extends Moe {
public void print(Moe p) {
System.out.println("Larry 1\n");
}
public void print(Larry l) {
System.out.println("Larry 2\n");
}
}
class Curly extends Larry {
public void print(Moe p) {
System.out.println("Curly 1\n");
}
public void print(Larry l) {
System.out.println("Curly 2\n");
}
public void print(Curly b) {
System.out.println("Curly 3\n");
}
}
public class Overloading_Final_Exam {
public static void main (String [] args) {
Larry stooge1 = …Run Code Online (Sandbox Code Playgroud) 许多人不喜欢使用instanceof,但我发现在很多情况下,我们在equals方法方面几乎没有其他选择.看看下面的课程:
class A {
int n;
public A(int n) { this.n = n; }
@Override
public boolean equals(Object o) {
return false;
}
public boolean equals(A o) {
return n == o.n;
}
}
Run Code Online (Sandbox Code Playgroud)
我从来没有见过像这样的东西,但它可以作为一个替代品,instanceof用来测试一个Object是不是A?或者还有其他我没想过的问题?
首先,我虽然java的多态函数是由它的参数实例类型映射的.
请有人帮忙解释为什么我的函数没有调用myFunction(EmployeeImpl emp)签名实例EmployeeImpl.
public class MainApp {
public static void main(String[] args){
Employee emp = new EmployeeImpl();
emp.callMyFunction();
}
}
abstract class Employee{
public void callMyFunction(){
//here is huge amount of code, which all child class has the same
//excepted this line is called to a different function by it instant types.
EmployeeFacade.myFunction(this);
}
}
class EmployeeImpl extends Employee{
}
class EmployeeFacade{
public static void myFunction(Employee emp){
//same data to database
System.out.println("Employee: "+ emp.getClass().getName());
}
public …Run Code Online (Sandbox Code Playgroud) java ×11
overloading ×4
equals ×2
inheritance ×2
binding ×1
casting ×1
instanceof ×1
oop ×1
overriding ×1
polymorphism ×1
reflection ×1
undefined ×1
upcasting ×1