基础
1 2 3 4 5
| public interface ICalculate { final float PI=3.14159f; float getArea(float r); float getCircumference(float r); }
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class Cire implements ICalculate { public float getArea(float r) { float area = PI*r*r; return area; } public float getCircumference(float r) { float circumference = 2*PI*r; return circumference; } }
|
1 2 3
| public interface ImageSaver { void save(); }
|
实现接口
1 2 3 4 5 6 7
| public class GIFSaver implements ImageSaver { @Override public void save() { System.out.println("将图片保存成GIF格式"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Car { private String name; private double speed; public double getSpeed() { return speed; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("车名:" + name + ","); sb.append("速度:" + speed + "千米/小时"); return sb.toString(); } }
|
定义接口
1 2 3 4
| import java.awt.Point; public interface GPS { public Point getLocation(); }
|
实现接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.awt.Point; public class GPSCar extends Car implements GPS { @Override public Point getLocation() { Point point = new Point(); point.setLocation(super.getSpeed(), super.getSpeed()); return point; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(super.toString()); sb.append(",坐标:(" + getLocation().x + "," + getLocation().y + ")"); return sb.toString(); } }
|
测试类
1 2 3 4 5 6 7 8 9 10
| import java.awt.Point; public class Test { public static void main(String[] args) { GPSCar gcar = new GPSCar(); Point point = gcar.getLocation(); System.out.println("坐标:(" + point.x + "," + point.y + ")"); String info = gcar.toString(); System.out.println(info); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Test { public static void main(String[] args) { Pigeon pigeon = new Pigeon(); System.out.println(pigeon.color); } }
class Bird { String color = "白色"; String skin = "羽毛"; }
class Pigeon extends Bird { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class Test { public static void main(String[] args) { Dog dog = new Dog(); System.out.println("执行dog.cry();语句时的输出结果:"); dog.cry(); Cat cat = new Cat(); System.out.println("执行cat.cry();语句时的输出结果:"); cat.cry(); Sheep sheep = new Sheep(); System.out.println("执行cat.cry();语句时的输出结果:"); sheep.cry(); } }
class Animal { public Animal() { } public void cry() { System.out.println("动物发出叫声!"); } }
class Dog extends Animal { public Dog() { } public void cry() { System.out.println("狗发出“汪汪...”声!"); } }
class Cat extends Animal { public Cat(){ } public void cry() { System.out.println("猫发出“喵喵...”声!"); } }
class Sheep extends Animal { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class Test { public static void main(String[] args) { Tiger tiger = new Tiger(); System.out.println("Tiger的皮肤:" + tiger.skin); tiger.test(); } }
class Beast { String skin = ""; public Beast() { } public Beast(String strSkin) { skin = strSkin; } public void move() { System.out.println("跑"); } }
class Tiger extends Beast { public Tiger() { super("条纹"); } public void test() { System.out.println("父类的肤色:" + super.skin); System.out.println("父类的动作跑:"); super.move(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| 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("明日科技"); manager.setSalary(3000); manager.setBirthday(new Date()); manager.setBonus(2000); System.out.println("员工的姓名:" + employee.getName()); System.out.println("员工的工资:" + employee.getSalary()); System.out.println("员工的生日:" + employee.getBirthday()); System.out.println("经理的姓名:" + manager.getName()); System.out.println("经理的工资:" + manager.getSalary()); System.out.println("经理的生日:" + manager.getBirthday()); System.out.println("经理的奖金:" + manager.getBonus()); } }
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; } public void setSalary(double salary) { this.salary = salary; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
class Manager extends Employee { private double bonus; public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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()); } }
class Employee { public String getInfo() { return "父类:我是明日科技的员工!"; } }
class Manager extends Employee { @Override public String getInfo() { return "子类:我是明日科技的经理!"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Test { final float PI=3.14159f; public float getArea(float r) { float area = PI*r*r; return area; } public float getArea(float l, float w) { float area = l*w; return area; } public void draw(int num) { System.out.println("画" + num + "个任意形状的图形"); } public void draw(String shape) { System.out.println("画一个" + 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+" 宽为"+w+"的矩形的面积是:" + areaRectangle); float r = 7; float areaCirc = test.getArea(r); System.out.println("求半径为"+r+"的圆的面积是:"+areaCirc); int num=7; test.draw(num); test.draw("三角形"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class Test { public static void main(String[] args) { Circle circle = new Circle(1); System.out.println("图形的名称是:" + circle.getName()); System.out.println("图形的面积是:" + circle.getArea()); Rectangle rectangle = new Rectangle(1, 1); System.out.println("图形的名称是:" + rectangle.getName()); System.out.println("图形的面积是:" + rectangle.getArea()); } }
abstract class Shape { public String getName() { return this.getClass().getSimpleName(); } public abstract double getArea(); }
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); } }
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; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class Test { public static void main(String[] args) { System.out.println("顾客要购买BMW:"); Car bmw = CarFactory.getCar("BMW"); System.out.println("提取汽车:" + bmw.getInfo()); System.out.println("顾客要购买Benz:"); Car benz = CarFactory.getCar("Benz"); System.out.println("提取汽车:" + benz.getInfo()); } }
abstract class Car { public abstract String getInfo(); }
class BMW extends Car { @Override public String getInfo() { return "BMW"; } }
class Benz extends Car { @Override public String getInfo() { return "Benz"; } }
class CarFactory { public static Car getCar(String name) { if (name.equalsIgnoreCase("BMW")) { return new BMW(); } else if (name.equalsIgnoreCase("Benz")) { return new Benz(); } else { return null; } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class Employee implements Comparable<Employee> { private int id; private String name; private int age; public Employee(int id, String name, int age) { 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("员工的编号:" + id + ","); sb.append("员工的姓名:" + name + ","); sb.append("员工的年龄:" + age); return sb.toString(); } public static void main(String[] args) { Employee employee1 = new Employee(1000,"学志", 27); Employee employee2 = new Employee(1001,"珊妮", 27); System.out.println("比较结果:" + employee1.compareTo(employee2)); System.out.println("员工信息:" + employee1.toString()); } }
|