欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

java面向對象知識點和列子

張凱麗1年前7瀏覽0評論

Java是一門面向對象的編程語言,它支持封裝、繼承和多態等面向對象的編程特性。下面我們來介紹一下Java面向對象的一些重要知識點。

類和對象

public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void sayHello() {
System.out.println("Hello, my name is " + name);
}
}
Person person = new Person("張三", 20);
System.out.println("姓名:" + person.getName() + ",年齡:" + person.getAge());
person.sayHello();

繼承

public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
public class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void meow() {
System.out.println("Meow~");
}
}
Cat cat = new Cat("Tom");
cat.eat();
cat.meow();

多態

public interface Animal {
void eat();
}
public class Cat implements Animal {
public void eat() {
System.out.println("Cat is eating.");
}
public void meow() {
System.out.println("Meow~");
}
}
public class Dog implements Animal {
public void eat() {
System.out.println("Dog is eating.");
}
public void bark() {
System.out.println("Bark!");
}
}
Animal animal1 = new Cat();
Animal animal2 = new Dog();
animal1.eat();
animal2.eat();

以上就是Java面向對象的一些重要知識點及其代碼實現。在實際開發中,面向對象的編程思想能夠幫助我們更好地組織和管理代碼,并且提高代碼的可復用性和可維護性。