本文共 6775 字,大约阅读时间需要 22 分钟。
在Java中,类是用来描述一类事物的属性和行为的模板,对象则是类实例化后的一种具体存在。所有的东西都可以看作是对象,包括我们日常生活中的物品和抽象概念。
定义一个类的步骤如下:
以下是一个简单的Java类定义示例:
public class Telephone {    // 属性    float screen;    float cpu;        // 方法    public void call() {        System.out.println("打电话");    }    public void sendMessage() {        System.out.println("发短信");    }}   使用对象的步骤如下:
类名 对象名 = new 类名();对象名.属性对象名.方法();例如:
Telephone phone = new Telephone();phone.screen = 5; // 设置属性phone.call(); // 调用方法
注意:在同一变量名的情况下,局部变量具有更高的优先级。
构造方法用于初始化对象。构造方法的语法如下:
public 构造方法名(参数1,参数2) {    // 初始化代码}   默认会自动创建一个无参数的构造方法。以下是一个简单的构造方法示例:
public Telephone(float newScreen) {    screen = newScreen;}   静态变量属于类,而不是对象,可以通过类名直接访问。静态变量可以修饰变量、方法和代码块。
示例:
public class HelloWorld {    static String hobby = "shopping";    public static void main(String[] args) {        System.out.println(HelloWorld.hobby);        HelloWorld hello = new HelloWorld();        System.out.println(hello.hobby);        hello.hobby = "swimming";    }}   静态方法属于类,可以通过类名直接调用。静态方法不能直接访问非静态成员。
示例:
public class HelloWorld {    public static void print() {        System.out.println("打印");    }    public static void main() {        HelloWorld.print();        HelloWorld hello = new HelloWorld();        hello.print();    }}   静态初始化块用于给静态变量赋值,类加载时执行一次。
示例:
public class HelloWorld {    static int num3 = 83;    static {        num3 = 83;        System.out.println("通过静态初始化块为静态变量num3赋值");    }}   封装是面向对象的核心特性之一。通过封装,可以将类的内部信息隐藏起来,只允许通过公共方法进行操作和访问。
封装的实现步骤:
privategetter和setter方法getter和setter方法中添加属性控制语句示例:
public class HelloWorld {    private float screen;    public float getScreen() {        return screen;    }    public void setScreen(float newScreen) {        screen = newScreen;    }    public static void main(String[] args) {        HelloWorld hello = new HelloWorld();        hello.setScreen(20.1f);        float result = hello.getScreen();        System.out.println(result);    }}   包用于组织和管理Java文件,避免类名冲突。包名可以使用小写字母和.分隔。
示例:
package com.jason.music;class MyClassMusic {}   继承是Java中实现多态的基础,子类可以继承父类的属性和方法。
语法:
class 子类 extends 父类 {}   示例:
class Dog extends Animal {}   方法重写要求返回类型、方法名和参数类型与父类方法一致。
示例:
class Dog extends Animal {    @Override    public void eat() {        System.out.println("狗在吃东西");    }}   初始化顺序为:父类属性初始化 → 父类构造方法 → 子类属性初始化 → 子类构造方法。
示例:
public class Parent {    int x;    public Parent() {        System.out.println("父类构造方法");    }}public class Child extends Parent {    int y;    public Child() {        super();        System.out.println("子类构造方法");    }}   final可以修饰类、方法和变量,具有以下作用:
示例:
public final class Animal {}   super用于访问父类的属性和方法。
示例:
public class Dog extends Animal {    int age = 20;    public void eat() {        System.out.println("狗在吃东西");    }    public void method() {        System.out.println(super.age);        super.eat();    }}   多态包括引用多态和方法多态。
示例:
Animal obj1 = new Dog();obj1.eat(); // 调用Dog类的eat方法
instanceof检查,否则会抛出ClassCastException。示例:
Object obj = new Dog();if (obj instanceof Dog) {    Dog d = (Dog) obj;    System.out.println(d.age);}   抽象类可以定义抽象方法,子类必须实现这些方法。
示例:
public abstract class Animal {    public abstract void eat();}   接口定义了一组方法的规范,类可以实现多个接口。
示例:
public interface IPlayGame {    void playGame();}public class SmartPhone implements IPlayGame {    public void playGame() {        System.out.println("玩游戏");    }}   public static void main(String[] args) {    Chinese p = new Chinese();    American a = new American();    p.say();    a.say();    APerson p1 = new Chinese();    APerson a1 = new American();    p1.say();    a1.say();}   package com.jason;import java.util.Scanner;public class Vehicle {    private String name;    private int rentingPDay;    private int peopleCap;    private int cargoCap;    public Vehicle(String name, int rentingPDay, int peopleCap, int cargoCap) {        setName(name);        setRentingPDay(rentingPDay);        setPeopleCap(peopleCap);        setCargoCap(cargoCap);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getRentingPDay() {        return rentingPDay;    }    public void setRentingPDay(int rentingPDay) {        this.rentingPDay = rentingPDay;    }    public int getPeopleCap() {        return peopleCap;    }    public void setPeopleCap(int peopleCap) {        this.peopleCap = peopleCap;    }    public int getCargoCap() {        return cargoCap;    }    public void setCargoCap(int cargoCap) {        this.cargoCap = cargoCap;    }}public class DaDaMain {    public static void main(String[] args) {        Vehicle[] vehicles = {            new Vehicle("奥迪A4", 500, 4, 0),            new Vehicle("马自达6", 400, 4, 0),            new Vehicle("皮卡雪6", 450, 4, 2),            new Vehicle("金龙", 800, 20, 0),            new Vehicle("松花江", 400, 0, 4),            new Vehicle("依维柯", 1000, 0, 20)        };        Scanner scanner = new Scanner(System.in);        int chooseCar = 0;        double totalCost = 0;        int carCount = 0;        String selectedVp = "";        String selectedVc = "";        System.out.println("欢迎使用答答租车系统:");        System.out.println("您是否想租车? 1 是 0 否");        int choice = scanner.nextInt();        if (choice != 0) {            System.out.println("您可租车的类型及价目表:");            System.out.println("序号                  骑车名称                租金               容量");            for (int i = 0; i < vehicles.length; i++) {                System.out.println((i+1) + " " + vehicles[i].getName() + "            " + vehicles[i].getRentingPDay() + "元/天      " + vehicles[i].getPeopleCap() + "人");            }            System.out.println("\n请输入您要租的车辆类型序号(1-" + vehicles.length + "):");            int selectedType = scanner.nextInt() - 1;            selectedVp = vehicles[selectedType].getName();            carCount++;            totalCost += vehicles[selectedType].getRentingPDay();            System.out.println("请输入您要租车的天数:");            int days = scanner.nextInt();            totalCost *= days;            System.out.println("选择的车辆: " + selectedVp + ", 天数: " + days + ", 总费用: " + totalCost + "元");        }    }}  转载地址:http://qjmcz.baihongyu.com/