1. 面向?qū)ο蟾攀?/h2>
1.1 對象
對象是類的一個實例,可以有屬性和方法。屬性是對象的特征,方法是對象的行為。
1.2 類
類是對象的抽象,描述了具有共同屬性和行為的一組對象。
1.3 封裝
封裝是將屬性和方法封裝在類中,不允許外部直接訪問和修改對象的屬性,只能通過類的方法進行操作。封裝可以提高代碼的安全性和可維護性。
示例代碼:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1.4 繼承
繼承是一種機制,可以讓一個類繼承另一個類的屬性和方法。子類可以繼承父類的所有非私有成員(屬性和方法)。通過繼承,可以實現(xiàn)代碼的復(fù)用和層次化設(shè)計。
示例代碼:
public class Student extends Person {
private int grade;
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
1.5 多態(tài)
多態(tài)是一種特性,在面向?qū)ο缶幊讨?,它允許能夠處理不同類型對象的代碼在不同類型對象上執(zhí)行不同的操作。多態(tài)使得代碼更加靈活和可擴展。
示例代碼:
public class Animal {
public void sound() {
System.out.println("Animal makes sound");
}
}
public class Cat extends Animal {
public void sound() {
System.out.println("Cat meows");
}
}
public class Dog extends Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Cat();
Animal animal2 = new Dog();
animal1.sound(); // Output: Cat meows
animal2.sound(); // Output: Dog barks
}
}
2. 類
2.1 成員變量
成員變量是定義在類中的變量,可以被類的所有方法訪問。它們存儲對象的狀態(tài)信息。
示例代碼:
public class Student {
String name; // 成員變量
public void setName(String name) {
this.name = name;
}
public void display() {
System.out.println("Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.setName("John");
student.display(); // Output: Name: John
}
}
2.2 成員方法
成員方法是定義在類中的方法,可以通過對象調(diào)用。它們對對象的操作和行為進行定義和實現(xiàn)。
示例代碼:
public class Circle {
double radius; // 成員變量
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return 3.14 * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
circle.setRadius(5.0);
double area = circle.getArea();
System.out.println("Area: " + area); // Output: Area: 78.5
}
}
2.3 權(quán)限修飾符
權(quán)限修飾符用于控制類、變量和方法的訪問權(quán)限。Java提供了四種權(quán)限修飾符:private、default、protected和public。
示例代碼:
public class Person {
private String name; // 只能在本類中訪問
String gender; // 只能在本包中訪問
protected int age; // 只能在本包和子類中訪問
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
2.4 局部變量
局部變量是在方法、構(gòu)造方法或代碼塊中定義的變量,只在其所在的范圍內(nèi)有效。局部變量必須在使用前初始化。
示例代碼:
public class Calculator {
public int add(int num1, int num2) {
int sum = num1 + num2; // 局部變量
return sum;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result = calculator.add(5, 3);
System.out.println("Sum: " + result); // Output: Sum: 8
}
}
2.5 局部變量的有效范圍
局部變量的有效范圍是從其聲明的位置開始,到其所在的代碼塊結(jié)束。超出該范圍后,局部變量將不再可見。
示例代碼:
public class Rectangle {
public void calculateArea() {
int length = 10; // 局部變量
int width = 5; // 局部變量
int area = length * width;
System.out.println("Area: " + area); // Output: Area: 50
}
}
2.6 this關(guān)鍵字
this關(guān)鍵字用于引用當前對象,可以在類的成員方法中使用。它可以解決變量名沖突和區(qū)分實例變量和局部變量的問題。
示例代碼:
public class Car {
private String color; // 成員變量
public void setColor(String color) {
this.color = color; // 使用this關(guān)鍵字引用成員變量
}
public void displayColor() {
String color = "Blue"; // 局部變量
System.out.println("Local color: " + color); // Output: Local color: Blue
System.out.println("Instance color: " + this.color); // Output: Instance color: Red
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.setColor("Red");
car.displayColor();
}
}
3 類的構(gòu)造方法
構(gòu)造方法是一種特殊的方法,用于創(chuàng)建對象時進行對象的初始化操作。構(gòu)造方法的名稱必須與類名相同,并且沒有返回類型。
示例代碼:
public class Person {
private String name;
private int age;
// 構(gòu)造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// getter和setter方法
// ...
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
}
}
上述代碼創(chuàng)建了一個名為Person的類,并定義了一個帶有兩個參數(shù)的構(gòu)造方法。在main方法中,通過調(diào)用構(gòu)造方法創(chuàng)建了一個名為person的Person對象。
4 靜態(tài)變量和靜態(tài)方法
靜態(tài)變量是屬于類的變量,所有對象共享同一個靜態(tài)變量的值。靜態(tài)方法是屬于類的方法,可以直接通過類名調(diào)用,無需創(chuàng)建對象。
示例代碼:
public class Person {
private String name;
private static int count; // 靜態(tài)變量
public Person(String name) {
this.name = name;
count++;
}
public static void printCount() { // 靜態(tài)方法
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("Alice");
Person person2 = new Person("Bob");
Person.printCount(); // 直接通過類名調(diào)用靜態(tài)方法
}
}
上述代碼中,定義了一個名為Person的類,并包含一個靜態(tài)變量count和一個靜態(tài)方法printCount。在構(gòu)造方法中,每次創(chuàng)建對象時,count的值都會增加。在main方法中,創(chuàng)建了兩個Person對象,并通過Person.printCount()直接調(diào)用靜態(tài)方法。
輸出結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-605942.html
Count: 2
5. 類的主方法
主方法(即main方法)是Java程序的入口點,程序從這里開始執(zhí)行。主方法必須是public、static和void類型,并且接受一個String數(shù)組作為參數(shù)。
示例代碼:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
上述代碼中,定義了一個名為Main的類,并包含一個名為main的主方法。在主方法中,調(diào)用System.out.println打印"Hello, World!"。
輸出結(jié)果:
Hello, World!
6. 對象
6.1 對象的創(chuàng)建
在Java中,使用new關(guān)鍵字可以創(chuàng)建一個對象。創(chuàng)建對象時需要調(diào)用類的構(gòu)造方法來初始化對象的屬性和執(zhí)行其他操作。
示例代碼:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
}
}
上述代碼創(chuàng)建了一個名為person的Person對象,傳入了"name"和"age"參數(shù)來初始化對象的屬性。
6.2 訪問對象的屬性和行為
通過對象的引用,可以訪問對象的屬性和調(diào)用對象的方法。
示例代碼:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
person.printInfo();
}
}
上述代碼創(chuàng)建了一個名為person的Person對象,并調(diào)用了對象的printInfo()方法輸出對象的信息。
輸出結(jié)果:
Name: Alice
Age: 25
6.3 對象的引用
在Java中,創(chuàng)建對象時會在堆內(nèi)存中分配空間,并返回一個對象的引用。通過引用可以操作對象,包括訪問對象的屬性和調(diào)用對象的方法。
示例代碼:
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 void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("Alice", 25);
Person person2 = person1;
person2.setName("Bob");
System.out.println(person1.getName()); // Output: Bob
}
}
上述代碼中,創(chuàng)建了一個名為person1的Person對象,并將該對象的引用賦值給person2。然后通過person2對象的引用,修改了person1對象的name屬性,并輸出了person1對象的name屬性值。
輸出結(jié)果:
Bob
6.4 對象的銷毀
在Java中,對象的銷毀是由垃圾回收器負責的。一般情況下,當對象沒有被引用時,即沒有任何引用指向該對象,垃圾回收器會自動將該對象回收。
示例代碼:
public class Person {
// ...
public void finalize() {
System.out.println("Object is being destroyed");
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person = null; // 解除對象引用
System.gc(); // 請求垃圾回收器執(zhí)行
}
}
上述代碼中,在main方法中創(chuàng)建了一個Person對象,并將person變量的引用設(shè)置為null。然后通過System.gc()請求垃圾回收器執(zhí)行,當垃圾回收器執(zhí)行時,會調(diào)用Person類的finalize()方法輸出"Object is being destroyed"。文章來源:http://www.zghlxwxcb.cn/news/detail-605942.html
輸出結(jié)果:
Object is being destroyed
到了這里,關(guān)于對象之舞:Java類與對象的探索的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!