OOP

Great work! Let's review everything that we've learned about object-oriented programming in Java so far.

  • Class: a blueprint for how a data structure should function
  • Constructor: instructs the class to set up the initial state of an object
  • Object: instance of a class that stores the state of a class
  • Method: set of instructions that can be called on an object
  • Parameter: values that can be specified when creating an object or calling a method
  • Return value: specifies the data type that a method will return after it runs
  • Inheritance: allows one class to use functionality defined in another class

Instance
Mouse.java

public class Mouse extends Rodentia {

    String name;

    public Mouse(String name) {

        this.name = name;

    }

    public void eat() {

        System.out.println(name + " ate some cheese pizza!");

    }

    public void solveMaze(int minutes) {

        System.out.println(name + " solved the maze in " + minutes + " minutes!");

    }

    public static void main(String[] args) {

        Mouse ratly = new Mouse("Ratly");
        ratly.eat();
        ratly.solveMaze(3);
        ratly.order();

    }

}

Rodentia.java

public class Rodentia {

    public static void main(String[] args) {

    }

    public void order() {

        System.out.println("This animal belongs to the Rodentia order.");

    }

}

你可能感兴趣的:(OOP)