Rumah >Java >javaTutorial >Memahami Pewarisan di Jawa Melalui Contoh Praktikal
Warisan ialah konsep teras dalam pengaturcaraan berorientasikan objek (OOP) yang membenarkan satu kelas memperoleh sifat (atribut dan kaedah) kelas lain. Di Jawa, pewarisan dilaksanakan menggunakan kata kunci lanjutan dan mewakili perhubungan "is-a". Artikel ini menerangkan warisan di Jawa melalui contoh praktikal.
// Defining a class class Animal { // General attributes protected String colour; protected String breed; protected int age; // General methods public String sleep() { return "Both cats and dogs sleep"; } public String eat() { return "They also eat"; } // Constructor public Animal(String colour, String breed, int age) { this.colour = colour; this.breed = breed; this.age = age; } // Getters and setters public String getColour() { return colour; } public void setColour(String colour) { this.colour = colour; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } // Cat class inheriting from Animal class class Cat extends Animal { private String catName; public Cat(String colour, String breed, int age, String catName) { super(colour, breed, age); // Call the parent class constructor this.catName = catName; } public String getCatName() { return catName; } public void setCatName(String catName) { this.catName = catName; } public String catSound() { return "Cat meows!"; } } // Dog class inheriting from Animal class class Dog extends Animal { private String dogName; public Dog(String colour, String breed, int age) { super(colour, breed, age); } public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } public String dogSound() { return "Dog barks!"; } } public class Demo { public static void main(String[] args) { Cat myCat = new Cat("Brown", "Persian", 2, "Tom"); Dog myDog = new Dog("Black", "Labrador", 3); // Display Cat details System.out.println("Cat's Name: " + myCat.getCatName()); System.out.println("Cat's Colour: " + myCat.getColour()); System.out.println("Cat's Breed: " + myCat.getBreed()); System.out.println("Cat's Age: " + myCat.getAge()); System.out.println("Cat Sound: " + myCat.catSound()); System.out.println("Cat Behavior: " + myCat.eat() + " and " + myCat.sleep()); // Display Dog details System.out.println("Dog's Colour: " + myDog.getColour()); System.out.println("Dog's Breed: " + myDog.getBreed()); System.out.println("Dog's Age: " + myDog.getAge()); System.out.println("Dog Sound: " + myDog.dogSound()); } }
Cat's Name: Tom Cat's Colour: Brown Cat's Breed: Persian Cat's Age: 2 Cat Sound: Cat meows! Cat Behavior: They also eat and Both cats and dogs sleep Dog's Colour: Black Dog's Breed: Labrador Dog's Age: 3 Dog Sound: Dog barks!
GitHub saya
java repo
Atas ialah kandungan terperinci Memahami Pewarisan di Jawa Melalui Contoh Praktikal. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!