JAVA基础 - 类的构造与实例化

Posted kaimobile

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA基础 - 类的构造与实例化相关的知识,希望对你有一定的参考价值。

一个简单的demo,主要运用:

  • 抽象类,类的继承
  • 类的实例化,构造函数
  • @Override重写父类方法
package week4;

abstract class Person {
    void showFullName() {
        System.out.print("Person");
    }
}

class People extends Person{
    int age;
    final String name;
    People (int age, String name){
        this.age = age;
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return this.age;
    }
    public String getName() {
        return this.name;
    }
    public void showFullName() {
        System.out.println(this.name);
    }
}

class Student extends People {
    final int stuId;
    static int peopleType = 1;
    Student (int age, String name, int stuId){
        super(age,name);
        this.stuId = stuId;
    }
    public int getId() {
        return this.stuId;
    }
    @Override
    public void showFullName() {
        System.out.println("Student:"+this.name);
    }
    
}

class Book {
    final int bookId;
    int bookStatus;
    int bookPeopleType;
    int bookPeopleId;
    Book (int bookId){
        this.bookId = bookId;
        bookStatus = 1;
        bookPeopleType = 0;
        bookPeopleId = 0;
    }
    
    void borrowBook(Student a) {
        this.bookStatus = 0;
        this.bookPeopleType = Student.peopleType;
        this.bookPeopleId = a.stuId;
        System.out.println(a.name+" Borrows "+this.bookId+" Successful!");
    }
    void returnBook() {
        this.bookStatus = 1;
        this.bookPeopleType = 0;
        this.bookPeopleId = 0;
        System.out.println(this.bookId+" has been returned!");
    }
    void showStatus() {
        if(this.bookStatus == 0) {
            System.out.println("The book has been borrowed!");
        }else {
            System.out.println("You can borrow it now!");
        }
    }
}

public class Week4 {

    static Book book1 = new Book(123);
    static Book book2 = new Book(124);
    static Student stu1 = new Student(20,"Tom",20180001);
    static Student stu2 = new Student(19,"John",20180201);
    
    public static void main(String[] args) {
        //Show information about stu1
        stu1.showFullName();
        System.out.println(stu2.name);
        //Borrow a book
        book1.showStatus();
        book1.borrowBook(stu1);
        book1.showStatus();
        System.out.println(book1.bookPeopleId);
        //return the book
        book1.returnBook();
        book1.showStatus();
    }

}

 

以上是关于JAVA基础 - 类的构造与实例化的主要内容,如果未能解决你的问题,请参考以下文章

java类实例化内存过程与面向对象特征

Java基础篇之abstract

从Java虚拟机角度分析类的实例化顺序

Java类的定义与类的实例化

Java类的实例化对象成员在内存空间怎么分配,调用构造函数又是在内存中怎么分配

java中成员变量代码块构造函数运行顺序