Coding exercises--java-5-interface, inheritance and polymorphism __ajax

Source: Internet
Author: User
Tags class manager comparable

Welcome to visit the blog Basic Definition interface

Public interface Icalculate {
    final float pi=3.14159f;
    Float Getarea (float R);
    Float getcircumference (float R);
}
Implementing interfaces
Note: All methods (abstractions) to implement the interface
The public class Cire implements Icalculate {
    //the Circle area is public
    float Getarea (float r) {
        float = pi*r*r;
        return area;
    }
    Computes the circumference of the circle public
    float getcircumference (float r) {
        float circumference = 2*pi*r;
        return circumference
    }
}
Instances-save pictures in different formats
Defining interfaces
Public interface Imagesaver {
    void Save ();
}

Implementing interfaces

public class Gifsaver implements Imagesaver {
    //rewrite-polymorphic
    @Override public
    Void Save () {
        System.out.println (save picture in GIF format);
    }
Example-to increase the GPS positioning function for automobiles
Parent class
public class Car {
    private String name;
    private double speed;
    Omit Getxx () and SETXX () public
    double GetSpeed () {return
        speed;
    }
    @Override public
    String toString () {
        StringBuilder sb = new StringBuilder ();
        Sb.append ("Car name:" + name + ",");
        Sb.append ("Speed:" + speed + "kilometer/hour");
        return sb.tostring ();
    }

Defining interfaces

Import Java.awt.Point;
Public interface GPS {public point
    getLocation ();
}

Implementing interfaces

Import Java.awt.Point;
public class Gpscar extends car implements GPS {
    @Override public point
    getLocation () {point point
        = new Poin T ();
        Point.setlocation (Super.getspeed (), Super.getspeed ());
        return point;
    }

    @Override public 
    String toString () {
        StringBuilder sb = new StringBuilder ();
        Sb.append (Super.tostring ());
        Sb.append (", Coordinates: (" + getLocation (). x + "," + getLocation (). Y + ")");
        return sb.tostring ();
    }

Test class

Import Java.awt.Point;
public class Test {public
    static void Main (string[] args) {
        Gpscar gcar = new Gpscar ();
        Point point = Gcar.getlocation ();
        System.out.println ("Coordinates: (" + Point.x + "," + Point.y +) ");
        String info = gcar.tostring ();
        SYSTEM.OUT.PRINTLN (info);
    }
Inherited
public class Test {public
    static void Main (string[] args) {
        pigeon pigeon = new pigeon ();
        System.out.println (Pigeon.color);
    }

The parent class class
Bird {
    String color = "white";
    String skin = "feather";
}
Subclass-Inheritance (properties and methods)
class Pigeon extends Bird {
}
Inheritance-overriding
public class Test {public
    static void Main (string[] args) {
        Dog Dog = new Dog ();
        SYSTEM.OUT.PRINTLN ("The output of the execution dog.cry (); statement:");
        Dog.cry ();
        Cat cat = new Cat ();
        SYSTEM.OUT.PRINTLN ("The output of the execution cat.cry (); statement:");
        Cat.cry ();
        Sheep Sheep = new Sheep ();
        SYSTEM.OUT.PRINTLN ("The output of the execution cat.cry (); statement:");
        Sheep.cry ();
    }

The parent class
class Animal {public
    Animal () {} is public

    void Cry () {System.out.println ()
        The animal makes a call. ");
    }
}
Subclass Class
Dog extends Animal {public
    Dog () {

    } public

    void Cry () {
        System.out.println ("Dog issue" Bark ... "sound. ");
    }
}
Subclass Class
Cat extends Animal {public
    cat () {

    } public

    void Cry () {
        System.out.println ("Cat emit" Meow meow ... "sound. ");
    }
}
Subclass Class
Sheep extends Animal {

}
Calling constructors, member variables, and member methods of the parent class
public class Test {public
    static void Main (string[] args) {
        Tiger Tiger = new Tiger ();
        System.out.println ("Tiger skin:" + tiger.skin);
        Tiger.test ();
    }

The parent class
class Beast {
    String skin = "";
    Public Beast () {    //parameterless constructor} public
    Beast (String strskin) {
        skin = Strskin;
    }
    public void Move () {
        System.out.println ("Run");
    }
Subclass-Inheritance (properties and methods)
class Tiger extends Beast {public
    tiger () {
        super ("stripe");    Construct method with parameters with parent class
    } public
    void Test () {
        System.out.println ("Skin of Parent: + Super.skin");
        System.out.println ("The action of the parent class runs:");
        Super.move ();
    }
Instances-Differences between managers (subclasses) and employees (parent classes)
Import Java.util.Date;
        public class Test {public static void main (string[] args) {Employee employee = new Employee ();
        Employee.setname ("Java");
        Employee.setsalary (100);
        Employee.setbirthday (New Date ());
        Manager Manager = new manager ();
        Manager.setname ("Tomorrow Technology");
        Manager.setsalary (3000);
        Manager.setbirthday (New Date ());
        Manager.setbonus (2000);
        Output Manager and Employee Attribute values System.out.println ("Employee's name:" + employee.getname ());
        SYSTEM.OUT.PRINTLN ("Employee's Salary:" + employee.getsalary ());
        SYSTEM.OUT.PRINTLN ("Employee's Birthday:" + employee.getbirthday ());
        System.out.println ("Manager's name:" + manager.getname ());
        System.out.println ("Manager's Salary:" + manager.getsalary ());
        System.out.println ("Manager's Birthday:" + manager.getbirthday ());
    System.out.println ("Manager's Bonus:" + Manager.getbonus ());
    }///Parent class class Employee {private String name;
    private double salary;
    Private Date birthday; Public String GetnamE () {return name;
    public void SetName (String name) {this.name = name;
    Public double getsalary () {return salary;
    The public void Setsalary (double salary) {this.salary = salary;
    Public Date Getbirthday () {return birthday;
    The public void Setbirthday (Date birthday) {this.birthday = birthday;
    }//Subclass-Inheritance (Properties and methods) class Manager extends Employee {private double bonus;
    Public double Getbonus () {return bonus;
    The public void Setbonus (double bonus) {This.bonus = bonus; }
}
Instance-overriding methods in the parent class
public class Test {public
    static void Main (string[] args) {
        Employee employee = new Employee ();
        System.out.println (Employee.getinfo ());
        Manager Manager = new manager ();
        System.out.println (Manager.getinfo ());
    }

Parent class
Employee {public
    String GetInfo () {return
        "parent": I'm an employee of tomorrow's technology. ";
    }
}
Subclass-Override
class Manager extends Employee {
    @Override public
    String GetInfo () {return
        "subclass: I'm the manager of tomorrow's technology. ";
    }
}
Polymorphic
public class Test {
    final float pi=3.14159f;
    To find the area of the circle public
    float Getarea (float r) {
        float = pi*r*r;
        return area;
    }
    To find the area of the rectangle public
    float Getarea (float l, float W) {
        float areas = l*w;
        return area;
    }
    Draw a graphic of any shape public
    void draw (int num) {
        System.out.println ("draw" + num + "freeform shape");
    }
    Draws the specified shape's graphics public
    void Draw (String shape) {
        System.out.println ("Draw one" + shape);
    }

    public static void Main (string[] args) {
        test test = new test ();
        float l=20;
        float w=30;
        float Arearectangle = Test.getarea (l, W);
        System.out.println ("+l+" width to "+w+" the area of the rectangle is: "+ arearectangle);
        float r = 7;
        float Areacirc = Test.getarea (r);
        System.out.println ("The radius is" +r+ "the area of the circle is:" +areacirc);
        int num=7;
        Test.draw (num);
        Test.draw ("triangle");
    }
Instance-Calculates the area of the geometry
public class Test {public static void main (string[] args) {Circle Circle = new Circle (1);
        System.out.println ("Graphic name is:" + circle.getname ());

        System.out.println ("The area of the graph is:" + Circle.getarea ());
        Rectangle Rectangle = new Rectangle (1, 1);
        System.out.println ("Graphic name is:" + rectangle.getname ());
    System.out.println ("The area of the graph is:" + Rectangle.getarea ());
    }///Parent abstract class Shape {public String getName () {return This.getclass (). Getsimplename ();//Get the name of the graphic   public abstract double Getarea ();
    Get the area of the graphic}//Subclass class Circle extends shape {private double radius;
    Public Circle (double radius) {This.radius = radius;
    @Override public Double Getarea () {return Math.pi*math.pow (RADIUS, 2);
    }//Subclass class Rectangle extends Shape {private double length;
    private double width;
        Public Rectangle (double length, double width) {this.length = length; This.width =Width
    @Override public Double Getarea () {return length * width; }
}
Example-Simple car Sales Mall
public class Test {public
    static void Main (string[] args) {
        System.out.println ("customer wants to buy BMW:");
        Car BMW = Carfactory.getcar ("BMW");
        System.out.println ("Extraction car:" + bmw.getinfo ());

        System.out.println ("Customers want to buy Benz:");
        Car Benz = Carfactory.getcar ("Benz");
        System.out.println ("Extraction car:" + benz.getinfo ());
    }

Parent abstract class Car
{public
    abstract String getInfo ();
}
Subclass class
BMW extends car {
    @Override public
    String GetInfo () {return
        "BMW";
    }
}
Subclass class
Benz extends car {
    @Override public
    String GetInfo () {return
        "Benz";
    }
}
Factory
class Carfactory {public
    static car Getcar (String name) {
        if (name.equalsignorecase ("BMW")) { Return to
            new BMW ();
        } else if (name.equalsignorecase ("Benz")) {return
            new Benz ():
        } else {return
            null;
        }
}}
Instance-custom sort using the comparable interface
public class Employee implements comparable<employee> {private int id;
    private String name;
    private int age;
        public Employee (int ID, String name, int age) {//constructor this.id = ID;
        THIS.name = name;
    This.age = age;
        @Override public int compareTo (Employee o) {if (id > O.id) {return 1;
        else if (ID < o.id) {return-1;
    return 0;
        @Override public String toString () {StringBuilder sb = new StringBuilder ();
        Sb.append ("Employee's number:" + ID + ",");
        Sb.append ("Employee's name:" + name + ",");
        Sb.append ("Age of the Employee:" + aged);
    return sb.tostring ();
        public static void Main (string[] args) {Employee employee1 = new Employee (1000, "Learning Annals", 27);
        Employee Employee2 = new Employee (1001, "Sunny", 27);
        System.out.println ("Comparison result:" + Employee1.compareto (employee2));
 SYSTEM.OUT.PRINTLN ("Employee Information:" + employee1.tostring ());   }
} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.