java中的介面一些知識點———— 程式碼

花朝666發表於2020-10-26

一 ;
nterface Shape {

public abstract void draw();

// default public void func(){
//
// }
}

class Cycle implements Shape{
public void draw(){
System.out.println(" ⭕");
}
}
class Flower implements Shape{
public void draw(){
System.out.println(" ❀");
}
}

public class TestLong {

public static  void drawMap(Shape shape){
    shape.draw();
}

public static void main (String[] args){
    Shape shape = new Flower();
    shape.draw();
    Shape shape1 = new Cycle();
    shape1.draw();
    Flower flower = new Flower();
    drawMap(flower);
}

}
二;
interface IA{
void func();

}

abstract class A implements IA{
public void fun(){
System.out.println(“4444”);
}
}
interface IB extends IA{
void fun2();
}

class C implements IB{
public C() {
super();
}

@Override
public void func() {

}

@Override
public void fun2() {

}

}
interface ID{
public void fun3();
}
interface IC extends IB ,ID{
@Override
void func();

@Override
void fun2();

@Override
void fun3();

}

class Animal {
protected String name ;

public  Animal (String name){
    this.name = name;
}

}
interface IFlying{
void fly();

}
interface IRunnig {
void run();
}
interface ISwimming{
void swim();
}

class Cat extends Animal implements IRunnig {
public Cat(String name) {
super(name);
}

@Override
public void run() {
    System.out.println(this.name + " 正在跑");
}

}
class Fish extends Animal implements ISwimming{
public Fish(String name){
super(name);
}

@Override
public void swim() {
    System.out.println(this.name + " 正在用尾巴游泳");
}

}
class Forg extends Animal implements IRunnig, ISwimming{
public Forg(String name){
super(name);
}

@Override
public void swim() {
    System.out.println(this.name + " 正在游泳" );
}

@Override
public void run() {
    System.out.println(this.name + " 正在跑步");
}

}
class RoBot implements IRunnig{
@Override
public void run() {
System.out.println(“機器人再跑”);
}
}
public class TestDemo {
public static void function1(IRunnig iRunnig){
iRunnig.run();
}
public static void function2(ISwimming iSwimming) {
iSwimming.swim();
}
public static void function3(IFlying iFlying){
iFlying.fly();
}
public static void main (String[] args){
function2(new Fish(“HAHHAHA”));

}
public static void main1 (String[] args){
Animal animal = new Fish("HAHHAHA");
ISwimming iSwimming = new Fish("HHHHHHHH");
}

}
三 ;
class Student implements Comparable{
public int age;
public String name;
public int score;

public Student(int age, String name, int score) { // alt + insert
    this.age = age;
    this.name = name;
    this.score = score;
}

@Override
public String toString() {
    return "Student{" +
            "age=" + age +
            ", name='" + name + '\'' +
            ", score=" + score +
            '}';
}

@Override
public int compareTo(Student o) {
    if (this.age < o.age){
        return  -1;
    }else if(this.age > o.age){
        return 1;
    }else {
        return 0;
    }
}

}
public class TestCheng {
public static void main3 (String[] args){
Student student1 = new Student(10,“lallal”,65);
Student student2 = new Student(34,“laal”,63);

    if (student1.compareTo(student2) > 0){
        System.out.println("student1 > student2");
    }else if(student1.compareTo(student2) < 0){
        System.out.println("student1 < student2");
    }else {
        System.out.println("student1 == student2");
    }

}
public static void main (String[] args){
    Student[] students = new Student[3];
    students[0] = new Student(10,"lallal",65);
    students[1] = new Student(34,"laal",63);
    students[2] = new Student(20,"lal",85);
    System.out.println(Arrays.toString(students));
    Arrays.sort(students);
    System.out.println(Arrays.toString(students));
}
public static void main1 (String[] args){
    int[] array  = {1,2,3,3,4,2,4,35,45,4};
    Arrays.sort(array);
    System.out.println(Arrays.toString(array));
}

}
四;
class Money implements Cloneable{
public double money = 34.5;

@Override
protected Object clone() throws CloneNotSupportedException {
    return super.clone();
}

}
class Person implements Cloneable{
public String name = " lalal";
public Money m = new Money();

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            '}';
}

@Override
protected Object clone() throws CloneNotSupportedException {
    Person person = (Person) super .clone();
     person.m = (Money) person.m.clone();
     return  person;
    //return super.clone();
}

}
public class TestXi {
public static void main (String[] args)throws CloneNotSupportedException {
Person person = new Person();
Person person1 = (Person) person.clone();
System.out.println(person.m.money);
System.out.println(person1.m.money);
System.out.println("============");
person1.m.money = 99.99;
System.out.println(person.m.money);
System.out.println(person1.m.money);

}

}

相關文章