public static void main(String[] args) { Breeder breeder = new Breeder(); Dog dog = breeder.breedDog(); dog.bark(); dog.fetch(); dog.bark(); dog.fetch(); }
public Animal getElement(int index) { Animal ani = animals.get(index); switch (ani.getSort()) { case DEER: Deer deer = (Deer) ani; return (Deer) deer.clone(); case DOG: Dog dog = (Dog) ani; return (Dog) dog.clone(); case GIFRAFFE: Gifraffe gifraffe = (Gifraffe) ani; return (Gifraffe) gifraffe.clone(); case HORSE: Horse horse = (Horse) ani; return (Horse) horse.clone(); case LION: Lion lion = (Lion) ani; return (Lion) lion.clone(); case WOLF: Wolf wolf = (Wolf) ani; return (Wolf) wolf.clone(); case CHEETAH: Cheetah cheetah = (Cheetah) ani; return (Cheetah) cheetah.clone(); default: // Don't know that type of creature. return (Animal) ani.clone(); } }
public static void foo(Dog d) { System.out.println(d.getName().equals("Max")); // true d.setName("superDog"); System.out.println(d.getName().equals("Max")); // true d = new Dog("Fifi"); System.out.println(d.getName().equals("Fifi")); // true }
public static void main(String[] args) { Animal animal = new Animal("Animal", 1, 1, 5, 5); // definition for a base Animal Dog dog = new Dog("Yorkie", 8, 20, 2, 4, 1, 20, "long silky"); // initializing a Dog dog.eat(); dog.walk(); dog.run(); }
public static void main(String[] args) { Animal a = new Animal(); Animal b = new Dog(); // b 是Animal 的一个引用 不用调用父类没有的方法 要想调用自己的方法 则需要创建本类的对象 a.move(); b.move(); Dog dog = new Dog(); dog.bark(); }
@Test public void testConstructorInjectionPoint() throws Throwable { Dog.reset(); getCurrentManager().getInstanceByType(ConstructorInjectionPointConsumer.class).ping(); InjectionPoint injectionPoint = Dog.getInjectionPoint(); InjectionPoint injectionPoint1 = deserialize(serialize(injectionPoint)); assert checkInjectionPoint(injectionPoint1, ConstructorInjectionPointConsumer.class); assert checkEquality(injectionPoint, injectionPoint1); }
@Test public void testMethodInjectionPoint(MethodInjectionPointConsumer consumer) throws Throwable { Dog.reset(); consumer.ping(); InjectionPoint injectionPoint = Dog.getInjectionPoint(); InjectionPoint injectionPoint1 = Utils.deserialize(Utils.serialize(injectionPoint)); Assert.assertTrue(checkInjectionPoint(injectionPoint1, MethodInjectionPointConsumer.class)); Assert.assertTrue(checkEquality(injectionPoint, injectionPoint1)); }
public static void main(String[] args) { Cat cat = new Cat(); Dog dog = new Dog(); Fish fish = new Fish(); Woman woman = new Woman(); cat.owner = woman; dog.owner = woman; fish.owner = woman; }
public static void main(String[] args) throws Throwable { Cat cat; Dog dog; for (int i = 0; i < 50000; i++) { cat = new Cat(); dog = new Dog(); cat.finalize(); dog.finalize(); } }
public void test() { Poodle poo = new Poodle("Fifi"); Dog d = poo; System.out.println("d.draw()"); d.draw(); // compiler error System.out.println("\np.draw()"); Poodle p = (Poodle) d; p.draw(); }
public String getNameOfOldest() { Dog oldest = null; int age = Integer.MIN_VALUE; for (Dog d : pups) { if (d.getAge() > age) { oldest = d; age = d.getAge(); } } return oldest.getName(); }
public String getNameOfYoungest() { Dog youngest = null; int age = Integer.MAX_VALUE; for (Dog d : pups) { if (d.getAge() < age) { youngest = d; age = d.getAge(); } } return youngest.getName(); }
public static void main(String[] args) { SuperClass superclass = new SuperClass(); Bird bird = new Bird(); Dog dog = new Dog(); System.out.println(); superclass.sleep(); superclass.eat(); bird.sleep(); bird.eat(); dog.eat(); dog.sleep(); }
@Test public void testPropertyComparator() { Dog dog = new Dog(); dog.setNickName("mace"); Dog dog2 = new Dog(); dog2.setNickName("biscy"); PropertyComparator c = new PropertyComparator("nickName", false, true); assertTrue(c.compare(dog, dog2) > 0); assertTrue(c.compare(dog, dog) == 0); assertTrue(c.compare(dog2, dog) < 0); }
public Animal dequeueAny() { if (dogs.size() == 0) { return dequeueCats(); } else if (cats.size() == 0) { return dequeueDogs(); } Dog dog = dogs.peek(); Cat cat = cats.peek(); if (dog.isOlderThan(cat)) { return dequeueDogs(); } else { return dequeueCats(); } }
public static void main(String[] args) { Dog one = new Dog(); one.size = 70; Dog two = new Dog(); two.size = 8; Dog three = new Dog(); three.size = 35; one.bark(); two.bark(); three.bark(); }
public String getInfo(String info){ if(dog == null){ info = name + " har ingen hund"; }else{ info = name + " köper en hund som heter: " + dog.getName(); } return info; }
public static void main(String args[]) throws CloneNotSupportedException { DogClone dogClone = new DogClone(); dogClone.legCounts = 3; System.out.println("原来的克隆狗腿数量:" + dogClone.legCounts); System.out.println("原来的普通狗腿的数量:" + dogClone.dog); // Dog的toString方法返回的值。 DogClone dogClone1 = (DogClone) dogClone.clone(); dogClone1.legCounts = 2; Dog dog = dogClone1.dog; dog.changeLegCounts(); System.out.println("克隆后原来狗腿数量:" + dogClone.legCounts); /** 出现的结果是:8 原因很简单就是dog是一个引用,改变一个对象的话,会改变另一个对象。 */ System.out.println("克隆后原来普通狗的数量:" + dogClone.dog); System.out.println("克隆后克隆狗腿的数量:" + dogClone1.legCounts); /** 改变源:改变了自身dogClone.dog,影像了对象dogClone.dog 的值, */ System.out.println("克隆后普通狗的数量:" + dogClone1.dog); }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pup); // get pup data from the intent int pupnum = (Integer) getIntent().getExtras().get("animalid"); String type = (String) getIntent().getExtras().get("animaltype"); Dog dog = Dog.pups[pupnum]; // populate image ImageView pupImage = (ImageView) findViewById(R.id.pupView); pupImage.setImageResource(dog.getImageResourceID()); // populate name TextView pupName = (TextView) findViewById(R.id.pup_name); pupName.setText(dog.getName()); }
public static void main(String args[]) throws Exception { LinkedList<Dog> dogs = new LinkedList<Dog>(); Dog a1 = new Solution06().new Dog("a1"); Dog b1 = new Solution06().new Dog("b1"); Dog c1 = new Solution06().new Dog("c1"); a1.setOrder(1); b1.setOrder(3); c1.setOrder(5); dogs.add(a1); dogs.add(b1); dogs.add(c1); LinkedList<Cat> cats = new LinkedList<Cat>(); Cat a2 = new Solution06().new Cat("a2"); Cat b2 = new Solution06().new Cat("b2"); Cat c2 = new Solution06().new Cat("c2"); a2.setOrder(2); b2.setOrder(4); c2.setOrder(6); cats.add(a2); cats.add(b2); cats.add(c2); AnimalQueue zoo = new AnimalQueue(); zoo.enqueue(a1); zoo.enqueue(a2); zoo.enqueue(b1); zoo.enqueue(b2); zoo.enqueue(c1); zoo.enqueue(c2); System.out.println(zoo.dequeueAny().name); System.out.println(zoo.dequeueCats().name); System.out.println(zoo.dequeueDogs().name); }
public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) "Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow!______________________ // c = Cat@54387593 d.greet(); // (C) Dog Fido says: WOOF! a = c; // a = Cat@54387593 ((Cat) a).greet(); // (D) Cat Garfield says: Meow! // a's static type: Animal // a's dynamic type: Cat a.greet(); // (E) Cat Garfield says: Meow! a = new Dog("Hieronymus", 10); d = (Dog) a; a = new Dog("Spot", 10); d = a; }
public static void main(String[] args) { Dog dog1 = new Dog(); dog1.bark(); dog1.name = "Bart"; Dog[] myDogs = new Dog[3]; myDogs[0] = new Dog(); myDogs[1] = new Dog(); myDogs[2] = dog1; myDogs[0].name = "Fred"; myDogs[1].name = "Marge"; System.out.println("last dog's name is "); System.out.println(myDogs[2].name); int x = 0; while (x < myDogs.length) { myDogs[x].bark(); x++; } }
public static void main(String[] args) { Dog d1 = new Dog(); Dog d2 = new Dog(); d1.name = "spot"; d1.say = "Ruff!"; d2.name = "scruffy"; d2.say = "Wurf!"; System.out.println("Dog1's name is '" + d1.name + "' and it says '" + d1.say + "'"); System.out.println("Dog2's name is '" + d2.name + "' and it says '" + d2.say + "'"); }
public static void main(String[] args) { Dog dog = new Dog(); dog.stroke(); dog.feed(); dog.stroke(); dog.stroke(); dog.stroke(); dog.stroke(); dog.stroke(); dog.stroke(); }
@SuppressWarnings("unchecked") @Test public void testCompoundComparatorInvert() { CompoundComparator<Dog> c = new CompoundComparator<Dog>(); c.addComparator(new PropertyComparator("lastName", false, true)); c.addComparator(new PropertyComparator("firstName", false, true)); Dog dog1 = new Dog(); dog1.setFirstName("macy"); dog1.setLastName("grayspots"); Dog dog2 = new Dog(); dog2.setFirstName("biscuit"); dog2.setLastName("grayspots"); assertTrue(c.compare(dog1, dog2) > 0); c.invertOrder(); assertTrue(c.compare(dog1, dog2) < 0); }
private void prepare() { Session s = openSession(); Transaction txn = s.beginTransaction(); polliwog = new Animal(); polliwog.setBodyWeight(12); polliwog.setDescription("Polliwog"); catepillar = new Animal(); catepillar.setBodyWeight(10); catepillar.setDescription("Catepillar"); frog = new Animal(); frog.setBodyWeight(34); frog.setDescription("Frog"); polliwog.setFather(frog); frog.addOffspring(polliwog); butterfly = new Animal(); butterfly.setBodyWeight(9); butterfly.setDescription("Butterfly"); catepillar.setMother(butterfly); butterfly.addOffspring(catepillar); s.save(frog); s.save(polliwog); s.save(butterfly); s.save(catepillar); Dog dog = new Dog(); dog.setBodyWeight(200); dog.setDescription("dog"); s.save(dog); Cat cat = new Cat(); cat.setBodyWeight(100); cat.setDescription("cat"); s.save(cat); zoo = new Zoo(); zoo.setName("Zoo"); Address add = new Address(); add.setCity("MEL"); add.setCountry("AU"); add.setStreet("Main st"); add.setPostalCode("3000"); zoo.setAddress(add); pettingZoo = new PettingZoo(); pettingZoo.setName("Petting Zoo"); Address addr = new Address(); addr.setCity("Sydney"); addr.setCountry("AU"); addr.setStreet("High st"); addr.setPostalCode("2000"); pettingZoo.setAddress(addr); s.save(zoo); s.save(pettingZoo); Joiner joiner = new Joiner(); joiner.setJoinedName("joined-name"); joiner.setName("name"); s.save(joiner); Car car = new Car(); car.setVin("123c"); car.setOwner("Kirsten"); s.save(car); Truck truck = new Truck(); truck.setVin("123t"); truck.setOwner("Steve"); s.save(truck); SUV suv = new SUV(); suv.setVin("123s"); suv.setOwner("Joe"); s.save(suv); Pickup pickup = new Pickup(); pickup.setVin("123p"); pickup.setOwner("Cecelia"); s.save(pickup); BooleanLiteralEntity bool = new BooleanLiteralEntity(); s.save(bool); txn.commit(); s.close(); }
@Test public void shouldHaveAName() { Dog coco = new Dog("Coco", null); assertThat(coco.getName(), is("Coco")); }
@Test public void shouldHaveABreed() { Dog max = new Dog(null, "Australian Shepard"); assertThat(max.getBreed(), is("Australian Shepard")); }
public static void main(String[] args) { Dog dog = new Dog(); System.out.println("+ Name: " + dog.getName()); }
@Test public void shouldBeAbleToHaveANameOtherThanCoco() { Dog benny = new Dog("Benny", null); assertThat(benny.getName(), is("Benny")); }