当前位置:实例文章 » JAVA Web实例» [文章]【Java】——面向对象编程基础

【Java】——面向对象编程基础

发布人:shili8 发布时间:2025-02-20 01:47 阅读次数:0

**Java 面向对象编程基础**

面向对象编程(Object-Oriented Programming,OOP)是计算机科学中的一种编程范式,它以类、对象、继承、多态、封装等概念为核心。Java 是一种支持面向对象编程的语言,下面我们将探讨 Java 面向对象编程的基础。

###1. 类和对象在 Java 中,类是用来描述一个事物或行为的模板,而对象则是根据这个类创建出来的具体实例。例如,我们可以定义一个 `Person` 类来描述一个人,然后根据这个类创建出多个 `Person` 对象,每个对象都有自己的属性和方法。

java// 定义 Person 类public class Person {
 private String name;
 private int age;

 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }

 public void sayHello() {
 System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
 }
}

// 根据 Person 类创建出多个对象public class Main {
 public static void main(String[] args) {
 Person person1 = new Person("John",25);
 Person person2 = new Person("Alice",30);

 person1.sayHello(); // Hello, my name is John and I am25 years old.
 person2.sayHello(); // Hello, my name is Alice and I am30 years old.
 }
}


###2. 继承继承是指一个类可以从另一个类中继承属性和方法。子类继承父类的特性,可以扩展或重写父类的行为。

java// 定义 Person 类public class Person {
 private String name;
 private int age;

 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }

 public void sayHello() {
 System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
 }
}

// 定义 Student 类,继承 Personpublic class Student extends Person {
 private String major;

 public Student(String name, int age, String major) {
 super(name, age); // 调用 Person 的构造函数 this.major = major;
 }

 public void sayHello() {
 System.out.println("Hello, my name is " + getName() + ", I am a student of " + getMajor());
 }
}

// 根据 Student 类创建出多个对象public class Main {
 public static void main(String[] args) {
 Student student1 = new Student("John",25, "Computer Science");
 Student student2 = new Student("Alice",30, "Mathematics");

 student1.sayHello(); // Hello, my name is John, I am a student of Computer Science student2.sayHello(); // Hello, my name is Alice, I am a student of Mathematics }
}


###3. 多态多态是指一个方法可以有多个不同的行为,取决于它的上下文。

java// 定义 Shape 类public class Shape {
 public void draw() {
 System.out.println("Drawing a shape...");
 }
}

// 定义 Circle 类,继承 Shapepublic class Circle extends Shape {
 @Override public void draw() {
 System.out.println("Drawing a circle...");
 }
}

// 定义 Rectangle 类,继承 Shapepublic class Rectangle extends Shape {
 @Override public void draw() {
 System.out.println("Drawing a rectangle...");
 }
}

// 根据 Shape、Circle 和 Rectangle 创建出多个对象public class Main {
 public static void main(String[] args) {
 Shape shape = new Shape();
 Circle circle = new Circle();
 Rectangle rectangle = new Rectangle();

 shape.draw(); // Drawing a shape...
 circle.draw(); // Drawing a circle...
 rectangle.draw(); // Drawing a rectangle...
 }
}


###4. 封装封装是指将数据和行为封闭在一个类中,使得外部世界无法直接访问这些数据和行为。

java// 定义 BankAccount 类public class BankAccount {
 private double balance;

 public void deposit(double amount) {
 balance += amount;
 }

 public void withdraw(double amount) {
 if (balance >= amount) {
 balance -= amount;
 } else {
 System.out.println("Insufficient funds!");
 }
 }

 public double getBalance() {
 return balance;
 }
}

// 根据 BankAccount 类创建出多个对象public class Main {
 public static void main(String[] args) {
 BankAccount account = new BankAccount();
 account.deposit(1000);
 account.withdraw(500);
 System.out.println("Balance: " + account.getBalance()); // Balance:500.0 }
}


以上就是 Java 面向对象编程的基础。通过这些例子和注释,你应该能够理解面向对象编程的核心概念,包括类、对象、继承、多态和封装。

相关标签:java开发语言
其他信息

其他资源

Top