public static void main(String[] args) { Father f = new Father(); f.fathMathod(); Father fs = new Son(); fs.fathMathod(); Son son = new Son(); son.fathMathod(); son.sonMathod(); System.out.println(son.father); /** 子类的对象不具有父类的私有的属性或方法 */ // System.out.println(son.prifat); /** 子类的对象不可以由父类产生 */ // Son sf = (Son) new Father(); // sf.fathMathod(); // sf.sonMathod(); double d = 8.0934d; int i = (int) d; int ii = 4; double dd = ii; Integer integer = new Integer(5); Double double1 = new Double(integer); System.out.println(d); System.out.println(i); System.out.println(dd); System.out.println(double1); }
/** @param args */ public static void main(String[] args) { Father f1 = new Father("f1"); Father f2 = new Father("f2"); Son s1 = new Son("s1"); Son s2 = new Son("s2"); Pair<Father> p1 = new Pair<Father>(f1, f2); Pair<Son> p2 = new Pair<Son>(s1, s2); printFather(p1); // 下面的语句会有编译错 // printFather(p2); // printSon(p1); printSon(p2); printAll(p1); printAll(p2); /* * 使用Pair<? extends Father>类型将不可以调用set方法 * 编译器只知道它需要某个Father的子类型,但是不知道具体是什么类型 * 它拒绝传递任何特定的类型;但get方法是安全可用的 */ Pair<? extends Father> pair = p2; // 下面的语句会有编译错 // pair.setFirst(s1); Father f3 = pair.getFirst(); System.out.println(f3.getName()); }
public Father getPatriarch() { Father elder = father; while (elder != null) { if (elder == this) { throw new IllegalStateException("Impossible state"); // fail fast during infinite loop } elder = elder.getFather(); } return elder; }
public String getRelationship(String name1, String name2, Person head) { Person firstPerson = personManager.getPersonWithName(head, name1); Person secondPerson = personManager.getPersonWithName(head, name2); String relation = Father.isFather(firstPerson, secondPerson) ? RelationEnum.FATHER.toString() : Mother.isMother(firstPerson, secondPerson) ? RelationEnum.MOTHER.toString() : Son.isSon(firstPerson, secondPerson) ? RelationEnum.SON.toString() : Daughter.isDaughter(firstPerson, secondPerson) ? RelationEnum.DAUGHTER.toString() : Brothers.isBrother(firstPerson, secondPerson) ? RelationEnum.BROTHERS.toString() : Sisters.isSister(firstPerson, secondPerson) ? RelationEnum.SISTERS.toString() : GrandDaughter.isGrandDaughter(firstPerson, secondPerson) ? RelationEnum.GRANDDAUGHTER.toString() : PaternalUncle.isPaternalUncle(firstPerson, secondPerson) ? RelationEnum.PATERNALUNCLE.toString() : PaternalAunt.isPaternalAunt(firstPerson, secondPerson) ? RelationEnum.PATERNALAUNT.toString() : MaternalUncle.isMaternalUncle( firstPerson, secondPerson) ? RelationEnum.MATERNALUNCLE.toString() : MaternalAunt.isMaternalAunt( firstPerson, secondPerson) ? RelationEnum.MATERNALAUNT.toString() : SisterInLaw.isSisterInLaw( firstPerson, secondPerson) ? RelationEnum.SISTERINLAW.toString() : BrotherInLaw.isBrotherInLaw( firstPerson, secondPerson) ? RelationEnum.BROTHERINLAW.toString() : Cousins.isCousin( firstPerson, secondPerson) ? RelationEnum.COUSINS.toString() : null; return relation; }
@Test public void testOrderedList() throws Exception { Session session = openSession(); Transaction tx = session.beginTransaction(); Child luke = new Child(); luke.setName("Luke"); Child leia = new Child(); leia.setName("Leia"); session.persist(luke); session.persist(leia); Father father = new Father(); father.getOrderedChildren().add(luke); father.getOrderedChildren().add(null); father.getOrderedChildren().add(leia); session.persist(father); tx.commit(); session.clear(); tx = session.beginTransaction(); father = (Father) session.get(Father.class, father.getId()); assertThat(father.getOrderedChildren()).as("List should have 3 elements").hasSize(3); assertThat(father.getOrderedChildren().get(0).getName()) .as("Luke should be first") .isEqualTo(luke.getName()); assertThat(father.getOrderedChildren().get(1)).as("Second born should be null").isNull(); assertThat(father.getOrderedChildren().get(2).getName()) .as("Leia should be third") .isEqualTo(leia.getName()); session.delete(father); session.delete(session.load(Child.class, luke.getId())); session.delete(session.load(Child.class, leia.getId())); tx.commit(); session.close(); checkCleanCache(); }
public int getWeightOfFather() { if (father == null) { return 0; } return father.getWeight(); }