编程语言
首页 > 编程语言> > Java语言程序设计与数据结构(基础篇)第十一章 继承和多态 学习笔记(持续更新中)

Java语言程序设计与数据结构(基础篇)第十一章 继承和多态 学习笔记(持续更新中)

作者:互联网

目录

课本笔记 第十一章

11.1 引言

11.2 父类和子类

程序清单 11-1 GeometricObject.java

public class GeometricObject {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    /**
     * Construct a default geometric object
     */
    public GeometricObject(){
        dateCreated = new java.util.Date();
    }
    /**
     * Construct a geometric object with the specified color and filled value
     */
    public GeometricObject(String color, boolean filled){
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }

    /**
     * Return color
     */
    public String getColor(){
        return color;
    }

    /**
     * Set a new color
     */
    public void setColor(String color){
        this.color = color;
    }

    /**
     * Return filled. Since filled is boolean
     * its getter method is named ifFilled
     */
    public boolean isFilled() {
        return filled;
    }
    /**
     * Set a new filled
     */
    public void setFilled(boolean filled) {
        this.filled = filled;
    }
    /**
     * Get dataCreated
     */
    public java.util.Date getDateCreated() {
        return dateCreated;
    }
    /**
     * Return a string representation of this object
     */
    public String toString(){
        return "created on " + dateCreated + "\ncolor: " + color +
                " and filled: " + filled;
    }
}

程序清单 11-2 Circle.java

public class Circle extends GeometricObject {
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public Circle(double radius, String color, boolean filled) {
        this.radius = radius;
        setColor(color);
        setFilled(filled);
    }

    /**
     * Return radius
     */
    public double getRadius() {
        return radius;
    }

    /**
     * Set a new radius
     */
    public void setRadius(double radius) {
        this.radius = radius;
    }

    /**
     * Return area
     */
    public double getArea() {
        return radius * radius * Math.PI;
    }

    /**
     * Return diameter
     */
    public double getDiameter() {
        return 2 * radius;
    }

    /**
     * Return perimeter
     */
    public double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    /**
     * Print the circle info
     */
    public void printCircle() {
        System.out.println("The circle is created " + getDateCreated()
                + " and the radius is " + radius);
    }
}

程序清单 11-3 Rectangle.java

public class Rectangle extends GeometricObject {
    private double width;
    private double height;

    public Rectangle() {
    }

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public Rectangle(double width, double height, String color, boolean filled) {
        this.width = width;
        this.height = height;
        setColor(color);
        setFilled(filled);
    }

    /**
     * Return width
     */
    public double getWidth() {
        return width;
    }

    /**
     * Set a new width
     */
    public void setWidth(double width) {
        this.width = width;
    }

    /**
     * Return height
     */
    public double getHeight() {
        return height;
    }

    /**
     * Set a new height
     */
    public void setHeight(double height) {
        this.height = height;
    }

    /**
     * Return area
     */
    public double getArea() {
        return width * height;
    }

    /**
     * Return perimeter
     */
    public double getPerimeter() {
        return 2 * (width + height);
    }
}

程序清单 11-4 TestCircleRectangle.java

public class TestCircleRectangle {
    public static void main(String[] args) {
        Circle circle = new Circle();
        System.out.println("A circle " + circle.toString());
        System.out.println("The color is " + circle.getColor());
        System.out.println("The radius is " + circle.getRadius());
        System.out.println("The area is " + circle.getArea());
        System.out.println("The diameter is " + circle.getDiameter());

        Rectangle rectangle = new Rectangle(2,4);
        System.out.println("\nA rectangle " + rectangle.toString());
        System.out.println("The area is " + rectangle.getArea());
        System.out.println("The perimeter is " + rectangle.getPerimeter());
    }
}

11.3 使用super关键字

11.3.1 调用父类方法

11.3.2 构造方法链

程序清单 11-5 PolymorphismDemo.java

public class PolymorphismDemo {
    /**
     * Main method
     */
    public static void main(String[] args) {
        //Display circle and rectangle properties
        displayObject(new Circle(1, "red", false));
    }

    /**
     * Display geometric object properties
     */
    private static void displayObject(GeometricObject object) {
        System.out.println("Created on " + object.getDateCreated() +
                ".Color is " + object.getColor());
    }
}

程序清单 11-6 DynamicBindingDemo.java

public class DynamicBindingDemo {
    public static void main(String[] args) {
        m(new GeometricObject());
        m(new Student());
        m(new Person());
        m(new Object());
    }

    public static void m(Object x) {
        System.out.println(x.toString());
    }
}

class GraduateStudent extends Student {
}

class Student extends Person {
    @Override
    public String toString() {
        return "Student";
    }
}

class Person extends Object {
    @Override
    public String toString() {
        return "Person";
    }
}

程序清单 11-7 CastingDemo.java

public class CastingDemo {
    /**
     * Main method
     */
    public static void main(String[] args) {
        //Create and initialize two objects
        Object object1 = new Circle(1);
        Object object2 = new Rectangle(1, 1);

        //Display circle and rectangle
        displayObject(object1);
        displayObject(object2);
    }

    /**
     * A method for display an object
     */
    public static void displayObject(Object object) {
        if (object instanceof Circle) {
            System.out.println("The circle area is " +
                    ((Circle) object).getArea());
            System.out.println("The circle diameter is " +
                    ((Circle) object).getDiameter());
        } else if (object instanceof Rectangle) {
            System.out.println("The rectangle area is " +
                    ((Rectangle) object).getArea());
        }
    }
}

程序清单 11-8 TestArrayList.java

import java.util.ArrayList;

public class TestArrayList {
    public static void main(String[] args) {
        //Create a list to store cities
        ArrayList<String> cityList = new ArrayList<>();

        //Add some cities in the list
        cityList.add("London");
        //cityList now contains [London]
        cityList.add("Denver");
        //cityList now contains [London,Denver]
        cityList.add("Paris");
        //cityList now contains [London,Denver,Paris]
        cityList.add("Miami");
        //cityList now contains [London,Denver,Paris,Miami]
        cityList.add("Seoul");
        //cityList now contains [London,Denver,Paris,Miami,Seoul]
        cityList.add("Tokyo");
        //cityList now contains [London,Denver,Paris,Miami,Seoul,Tokyo]

        System.out.println("List size? " + cityList.size());
        System.out.println("Is Miami in the list? " + cityList.indexOf("Denver"));
        //Print false
        System.out.println("Is the list empty? " + cityList.isEmpty());

        //Insert a new city at index2
        cityList.add(2, "Xian");
        //Contains [London,Denver,Xian,Paris,Miami,Seoul,Tokyo]

        //Remove a city from the list
        cityList.remove("Miami");
        //Contains [London,Denver,Xian,Paris,Seoul,Tokyo]

        //Remove a city from the list
        cityList.remove(1);
        //Contains [London,Xian,Paris,Seoul,Tokyo]

        //Display the contents in the list
        System.out.println(cityList.toString());

        //Display the contents in the list in reverse order
        for (int i = cityList.size() - 1; i >= 0; i--) {
            System.out.print(cityList.get(i) + " ");
        }
        System.out.println();

        //Create a list to store two circles
        ArrayList<Circle> list = new ArrayList<>();

        //Add two circles
        list.add(new Circle(2));
        list.add(new Circle(3));

        //Display the area of the first circle in the list
        System.out.println("The area of the circle? " + list.get(0).getArea());
    }
}

程序清单 11-9 DistinctNumbers.java

import java.util.ArrayList;
import java.util.Scanner;

public class DistinctNumbers {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        Scanner input = new Scanner(System.in);
        System.out.print("Enter integers (input ends with 0): ");
        int value;

        do {
            //Read a value from the input
            value = input.nextInt();
            if (!list.contains(value) && value != 0) {
                //Add the value if it is not in the list
                list.add(value);
            }
        } while (value != 0);

        //Display the distinct numbers
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
    }
}

程序清单 11-10 DistinctNumbers.java

import java.util.ArrayList;

public class MyStack {
    private ArrayList<Object> list = new ArrayList<>();

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public int getSize() {
        return list.size();
    }

    public Object peek() {
        return list.get(getSize() - 1);
    }

    public Object pop() {
        Object o = list.get(getSize() - 1);
        list.remove(getSize() - 1);
        return o;
    }

    public void push(Object o) {
        list.add(o);
    }

    @Override
    public String toString() {
        return "stack: " + list.toString();
    }
}

标签:数据结构,Java,构造方法,list,多态,Circle,父类,public,filled
来源: https://blog.csdn.net/weixin_47033153/article/details/119361559