【Java6】this/super/继承,抽象
发布日期:2021-04-30 21:05:57 浏览次数:110 分类:精选文章

本文共 6049 字,大约阅读时间需要 20 分钟。

Java??????????????????????????????????????????????????????????????????????

???????

?Java?????????????????????????????????????????????????????????

???Shape???Circle?

interface Shape {    void draw();    void fill();}
class Circle implements Shape {    @Override    public void draw() {        System.out.println("????");    }    @Override    public void fill() {        System.out.println("???");    }}

Circle???Shape???????Shape???????????????

????

?????Java????????????????????????????????????????

??????????

public class Test {    public static void main(String[] args) {        new Thread() {            @Override            public void run() {                System.out.println("?????");            }        }.start();    }}

?????????????????????????????

???????

?Java????????????????????????????????????????????????????????????

???Zi???Fu?

class Fu {    private String name;    public Fu(String name) {        this.name = name;    }}class Zi extends Fu {    public Zi(String name) {        super(name);    }}

Zi????????Fu???????????????

??????

??????????????????????????????????????????????????????????????

??????Animal????Dog

abstract class Animal {    String name;    abstract void eat();    abstract void run();}class Dog extends Animal {    @Override    void eat() {        System.out.println("?????");    }    @Override    public void run() {        System.out.println("????");    }}

Dog???Animal?????eat?run???

???????Account?

????????Account????????????????????????

public class Account {    private String id;    private double balance;    private double annualInterestRate;    public Account(String id, double balance, double annualInterestRate) {        this.id = id;        this.balance = balance;        this.annualInterestRate = annualInterestRate;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public double getBalance() {        return balance;    }    public void setBalance(double balance) {        this.balance = balance;    }    public double getAnnualInterestRate() {        return annualInterestRate;    }    public void setAnnualInterestRate(double annualInterestRate) {        this.annualInterestRate = annualInterestRate;    }    public double getMonthlyInterest() {        return annualInterestRate / 12;    }    public void withdraw(double amount) {        if (amount < 0) {            System.out.println("????");            return;        }        if (amount > balance) {            System.out.println("????");        } else {            balance -= amount;        }    }    public void deposit(double amount) {        if (amount < 0) {            System.out.println("????");            return;        }        balance += amount;    }}

TestAccount???Account?????

public class TestAccount {    public static void main(String[] args) {        Account account = new Account("11223344", 20000, 4.5 / 100);        account.withdraw(30000);        System.out.println("???" + account.getBalance());        account.withdraw(2500);        System.out.println("???" + account.getBalance());        account.deposit(3000);        System.out.println("???" + account.getBalance());        System.out.println("????" + account.getMonthlyInterest() * 100 + "%");    }}

?????????????Account??????????

Book??????

Book??????????????????????????

public class Book {    private Integer id;    private String title;    private String author;    private double price;    private Integer sales;    private Integer stock;    private String imgPath = "static/img/default.jpg";    public Book(Integer id, String title, String author, double price, Integer sales, Integer stock, String imgPath) {        this.id = id;        this.title = title;        this.author = author;        this.price = price;        this.sales = sales;        this.stock = stock;        this.imgPath = imgPath;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    public Integer getSales() {        return sales;    }    public void setSales(Integer sales) {        this.sales = sales;    }    public Integer getStock() {        return stock;    }    public void setStock(Integer stock) {        this.stock = stock;    }    public String getImgPath() {        return imgPath;    }    public void setImgPath(String imgPath) {        this.imgPath = imgPath;    }    public String getInfo() {        return id + "\t" + title + "\t\t" + author + "\t" + price + "\t" + sales + "\t" + stock + "\t" + imgPath;    }}

Test05???Book???

public class Test05 {    public static void main(String[] args) {        Book[] all = new Book[4];        all[0] = new Book(1, "?????", "???", 88, 100, 200);        all[1] = new Book(2, "?????", "???", 56, 800, 100);        all[2] = new Book(3, "??????", "???", 77, 500, 800);        all[3] = new Book(4, "?????", "???", 66, 54, 600, "static/img/shui.jpg");        for (int i = 0; i < all.length; i++) {            System.out.println(all[i].getInfo());        }        for (int i = 1; i < all.length; i++) {            for (int j = 0; j < all.length - i; j++) {                if (all[j].getSales() < all[j + 1].getSales()) {                    Book temp = all[j];                    all[j] = all[j + 1];                    all[j + 1] = temp;                }            }        }        System.out.println("----------------------------------");        System.out.println("??\t??\t\t??\t??\t??\t???\t??");        for (int i = 0; i < all.length; i++) {            System.out.println(all[i].getInfo());        }    }}

????????????????Book??????????

??

???????????Java??????????????????????????????????????????????Java???

上一篇:JDK、JRE、JVM三者之间的关系
下一篇:Java多线程4

发表评论

最新留言

表示我来过!
[***.240.166.169]2026年05月29日 22时47分35秒