3.1 类与对象:Java的心脏

Java是一种面向对象的编程语言,类和对象是其核心概念。

3.1.1 类的定义

类是对象的模板,定义了对象的属性和行为。在Java中,类使用class关键字定义。

  1. public class Person {
  2. String name;
  3. int age;
  4. void introduce() {
  5. System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
  6. }
  7. }

在这个例子中,Person类有两个属性:nameage,以及一个方法:introduce()

3.1.2 创建对象

对象是类的实例,可以通过new关键字来创建。

  1. public class Main {
  2. public static void main(String[] args) {
  3. Person person = new Person(); // 创建Person对象
  4. person.name = "Alice";
  5. person.age = 30;
  6. person.introduce(); // 输出:Hi, my name is Alice and I am 30 years old.
  7. }
  8. }

3.1.3 构造方法

构造方法用于初始化对象,方法名与类名相同,没有返回值。

  1. public class Person {
  2. String name;
  3. int age;
  4. // 构造方法
  5. public Person(String name, int age) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. void introduce() {
  10. System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
  11. }
  12. }
  13. // 使用构造方法创建对象
  14. public class Main {
  15. public static void main(String[] args) {
  16. Person person = new Person("Bob", 25);
  17. person.introduce(); // 输出:Hi, my name is Bob and I am 25 years old.
  18. }
  19. }

3.2 继承:站在巨人的肩膀上

继承是面向对象编程的重要特性,它允许我们创建一个新类,该新类继承现有类的属性和方法。

3.2.1 基本语法

使用extends关键字实现继承。

  1. public class Animal {
  2. void eat() {
  3. System.out.println("This animal eats food.");
  4. }
  5. }
  6. public class Dog extends Animal {
  7. void bark() {
  8. System.out.println("The dog barks.");
  9. }
  10. }
  11. // 使用继承
  12. public class Main {
  13. public static void main(String[] args) {
  14. Dog dog = new Dog();
  15. dog.eat(); // 输出:This animal eats food.
  16. dog.bark(); // 输出:The dog barks.
  17. }
  18. }

3.2.2 方法重写

子类可以重写父类的方法,使用@Override注解来标识。

  1. public class Animal {
  2. void eat() {
  3. System.out.println("This animal eats food.");
  4. }
  5. }
  6. public class Dog extends Animal {
  7. @Override
  8. void eat() {
  9. System.out.println("The dog eats bones.");
  10. }
  11. }
  12. // 方法重写
  13. public class Main {
  14. public static void main(String[] args) {
  15. Dog dog = new Dog();
  16. dog.eat(); // 输出:The dog eats bones.
  17. }
  18. }

3.3 封装:隐藏实现的艺术

封装是将对象的属性和方法私有化,并提供公共的方法来访问和修改这些属性。

3.3.1 使用访问修饰符

Java提供了四种访问修饰符:privatedefault(无修饰符)、protectedpublic

  1. public class Person {
  2. private String name;
  3. private int age;
  4. // 提供公共的getter和setter方法
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. public void setAge(int age) {
  15. this.age = age;
  16. }
  17. }

3.3.2 使用封装

  1. public class Main {
  2. public static void main(String[] args) {
  3. Person person = new Person();
  4. person.setName("Charlie");
  5. person.setAge(28);
  6. System.out.println("Name: " + person.getName()); // 输出:Name: Charlie
  7. System.out.println("Age: " + person.getAge()); // 输出:Age: 28
  8. }
  9. }

3.4 多态:形态多变的Java

多态是同一个方法在不同对象中表现出不同行为的能力。多态可以通过方法重写和接口实现。

3.4.1 方法重写中的多态

  1. public class Animal {
  2. void makeSound() {
  3. System.out.println("Some generic animal sound");
  4. }
  5. }
  6. public class Dog extends Animal {
  7. @Override
  8. void makeSound() {
  9. System.out.println("Bark");
  10. }
  11. }
  12. public class Cat extends Animal {
  13. @Override
  14. void makeSound() {
  15. System.out.println("Meow");
  16. }
  17. }
  18. // 多态示例
  19. public class Main {
  20. public static void main(String[] args) {
  21. Animal myDog = new Dog();
  22. Animal myCat = new Cat();
  23. myDog.makeSound(); // 输出:Bark
  24. myCat.makeSound(); // 输出:Meow
  25. }
  26. }

3.4.2 接口实现中的多态

  1. public interface Animal {
  2. void makeSound();
  3. }
  4. public class Dog implements Animal {
  5. @Override
  6. public void makeSound() {
  7. System.out.println("Bark");
  8. }
  9. }
  10. public class Cat implements Animal {
  11. @Override
  12. public void makeSound() {
  13. System.out.println("Meow");
  14. }
  15. }
  16. // 接口多态示例
  17. public class Main {
  18. public static void main(String[] args) {
  19. Animal myDog = new Dog();
  20. Animal myCat = new Cat();
  21. myDog.makeSound(); // 输出:Bark
  22. myCat.makeSound(); // 输出:Meow
  23. }
  24. }