java 继承&&抽象类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 继承&&抽象类相关的知识,希望对你有一定的参考价值。

import java.util.*;

abstract class Book {
    String title;
    String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    abstract void display();
}

//-----------------
/* An abstract class is a class that is declared abstract and may or may not include abstract methods. When an abstract class is subclassed, the subclass usually provides implementations for all the abstract metrods in the parent class. However, if it does not, then the subclass must also be declares abstract. */
//------------------

// Declare your class here. Do not use the 'public' access modifier.
class MyBook extends Book {
 // Declare the price instance variable
    int price;

    /**
    *   Class Constructor
    *
    *   @param title The book's title.
    *   @param author The book's author.
    *   @param price The book's price.
    **/
    // Write your constructor here
        MyBook(String title, String author, int price){
            super(title, author);
            this.price = price;
        }
    /**
    *   Method Name: display
    *
    *   Print the title, author, and price in the specified format.
    **/
    void display(){
        System.out.println("Title: " + title);
        System.out.println("Author: " + author);
        System.out.println("Price: " + price);
    }

// End class
}


public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String title = scanner.nextLine();
        String author = scanner.nextLine();
        int price = scanner.nextInt();
        scanner.close();

        Book book = new MyBook(title, author, price);
        book.display();
    }
}

以上是关于java 继承&&抽象类的主要内容,如果未能解决你的问题,请参考以下文章

interface多重继承&&byte&&抽象类

JAVA入门零基础小白教程day07_继承&抽象

JAVA入门零基础小白教程day07_继承&抽象

第七周Java总结&实验总结

java 抽象类&接口

第六周&java实验报告四