Java的实例化问题
import java.util.TreeSet;
public class TreeSetTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet set = new TreeSet();
//set.add(new Parent(3));
//set.add(new Parent(3));
set.add(new Child());
//set.add(new Child());
set.add(new Parent(4));
set.add(new Parent(4));
System.out.println(set.size());
}
}
class Parent implements Comparable {
private int age = 0;
public Parent(int age){
this.age = age;
}
public int compareTo(Object o) {
System.out.println("method of parent");
Parent o1 = (Parent)o;
return age>o1.age?1:age<o1.age?-1:0;
}
}
class Child extends Parent {
public Child(){
super(3);
}
public int compareTo(Object o) {
// TODO Auto-generated method stub
System.out.println("method of child");
//Child o1 = (Child)o;
return 1;
}
}
各位热心人看一下,子类实例化,但是父类还实现了接口,到底是什么一个顺序? Java 对象 实例
[解决办法]
关键还是看compareTo方法的执行的情况。
[解决办法]
我自己是这么分析的,首先先给你引入个重写的例子
public class TestParentChild
{
public static void main(String[] args)
{
new Test2().say();
}
}
class Test1
{
public void say()
{
System.out.println("Parent");
}
}
class Test2 extends Test1
{
@Override
public void say()
{
System.out.println("Child");
}
}
//结果:Child
再回来说你的那个例子
set.add(new Child());
就用Child里面的实现,Child继承自父类也实现了接口Comparable
通过这样Comparable comparable = (Comparable) new Child();可以验证
所以用子类的比较,所以输出method of child
返回1,对于添加第一个对象没什么可考虑的,添就行了。
set.add(new Parent(4));
同上面例子,添加父类就调父类的,因为和子类一点关系都没有啊(我是这么理解的)
所以就用父类的比较方法了,所以输出method of parent
不过你写这个连续套用的三元比较式我说的不一定准确了,看的很晕
我自己分析思路是按照新生成的这个对象去比较set里的对象
4>3 真,所以取1
返回1,排列在这个对象排列在set里的对象后面
set.add(new Parent(4));
再次添加父类,类似,不过多比较了个对象
新对象生成后比较set里面第一个对象输出一次method of parent,返回1;
新对象比较set里面第二个对象输出一次method of parent,返回0;
集合的特性不允许重复,所以不添加到set了
所以最终set.size()也就是2,
两个元素可以用System.out.println(set);看到
//[com.nyohh.test1.Child@39443f, com.nyohh.test1.Parent@1afae45]//包名可能不同
至于想看到内容是谁的话,可以添加个名字属性。看看具体添加的父类对象是第几次添加的。
package com.nyohh.test1;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetTest
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
TreeSet set = new TreeSet();
// set.add(new Parent(3));
// set.add(new Parent(3));
set.add(new Child());
// set.add(new Child());
set.add(new Parent(4,"zhangsan"));
set.add(new Parent(4,"lisi"));
System.out.println(set.size());
for (Object object : set)
{
Parent p=(Parent) object;
System.out.println(p.getName());
}
}
}
class Parent implements Comparable
{
private int age = 0;
public String name;
public Parent(int age,String name)
{
this.age = age;
this.name=name;
}
public String getName()
{
return name;
}
public int compareTo(Object o)
{
System.out.println("method of parent");
Parent o1 = (Parent) o;
int value= age > o1.age ? 1 : age < o1.age ? -1 : 0;
if (value==0)
{
System.out.println(this.name+"没有被添加进去,因为set的特性");
}
return value;
}
}
class Child extends Parent
{
public Child()
{
super(3,"system");
}
public int compareTo(Object o)
{
// TODO Auto-generated method stub
System.out.println("method of child");
// Child o1 = (Child)o;
return 1;
}
}
------解决方案--------------------
在你保存这个java的时候已经被编译为class文件了,你要实例化父子两个对象都是可以的,顺序是根据你实例的顺序,Treeset集合保存对象的顺序是有自己的机制的,不是根据你添加的顺序保存的。。
[解决办法]
是类的话基本都可以实现接口(除了final 抽象类),具体执行顺序,你得看main方法里面的执行顺序