loading

Java_7 类与接口

title:(在线学习平台) link:(https://www.acwing.com/) cover:(https://cdn.acwing.com/media/activity/surface/log.png)

1.类与对象

类定义一种全新的数据类型,包含一组变量和函数;对象是类这种类型对应的实例。

例如在一间教室中,可以将Student定义成类,表示“学生”这个抽象的概念。那么每个同学就是Student类的一个对象(实例)。

1.1 源文件声明规则

  • 一个源文件中只能有一个public类。
  • 一个源文件可以有多个非public类。
  • 源文件的名称应该和public类的类名保持一致。
  • 每个源文件中,先写package语句,再写import语句,最后定义类。

1.2 类的定义

  • public: 所有对象均可以访问
  • private: 只有本类内部可以访问
  • protected:同一个包或者子类中可以访问
  • 不添加修饰符:在同一个包中可以访问
  • 静态(带static修饰符)成员变量/函数与普通成员变量/函数的区别:
    • 所有static成员变量/函数在类中只有一份,被所有类的对象共享;
    • 所有普通成员变量/函数在类的每个对象中都有独立的一份;
    • 静态函数中只能调用静态函数/变量;普通函数中既可以调用普通函数/变量,也可以调用静态函数/变量。
class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public String toString() {
        return String.format("(%d, %d)", x, y);
    }
}

1.3 类的继承

每个类只能继承一个类。

class ColorPoint extends Point {
    private String color;

    public ColorPoint(int x, int y, String color) {
        super(x, y);
        this.color = color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String toString() {
        return String.format("(%d, %d, %s)", super.getX(), super.getY(), this.color);
    }
}

1.4 类的多态

public class Main {
    public static void main(String[] args) {
        Point point = new Point(3, 4);
        Point colorPoint = new ColorPoint(1, 2, "red");

        // 多态,同一个类的实例,调用相同的函数,运行结果不同
        System.out.println(point.toString());
        System.out.println(colorPoint.toString());
    }
}

2 接口

interfaceclass类似。主要用来定义类中所需包含的函数。

接口也可以继承其他接口,一个类可以实现多个接口

2.1 接口的定义

  • 接口中不添加修饰符时,默认为public
  • 接口中的方法,实现类里必须都要实现。
  • idea快捷键alt+enter
  • 一个类实现了某个接口,就可以把这个类的对象放在这个接口的引用上,即接口的多态。
interface Role {
    public void greet();
    public void move();
    public int getSpeed();
}

2.2 接口的继承

每个接口可以继承多个接口

interface Hero extends Role {
    public void attack();
}

2.3 接口的实现

每个类可以实现多个接口

class Zeus implements Hero {
    private final String name = "Zeus";
    public void attack() {
        System.out.println(name + ": Attack!");
    }

    public void greet() {
        System.out.println(name + ": Hi!");
    }

    public void move() {
        System.out.println(name + ": Move!");
    }

    public int getSpeed() {
        return 10;
    }
}

2.4 接口的多态

class Athena implements Hero {
    private final String name = "Athena";
    public void attack() {
        System.out.println(name + ": Attack!!!");
    }

    public void greet() {
        System.out.println(name + ": Hi!!!");
    }

    public void move() {
        System.out.println(name + ": Move!!!");
    }

    public int getSpeed() {
        return 10;
    }
}

public class Main {
    public static void main(String[] args) {
		
		//Hero是接口,Zeus, Athena是实现它的类
        Hero[] heros = {new Zeus(), new Athena()};
        for (Hero hero: heros) {
            hero.greet();
        }
    }
}

3 习题

3.1 三元组排序

image

  • 输入样例
5
32 1.36 nsyiupnnhc
18 4.53 fmofzwrah
33 4.86 wzuymbm
1 3.93 gtnrwcebt
31 4.53 gcllxioc
  • 输出样例
1 3.93 gtnrwcebt
18 4.53 fmofzwrah
31 4.53 gcllxioc
32 1.36 nsyiupnnhc
33 4.86 wzuymbm
  • 题解
import java.util.Arrays;
import java.util.Scanner;



class Data implements  Comparable<Data>{
    int a;
    double b;
    String c;

    Data(int a,double b,String c){
        this.a=a;
        this.b=b;
        this.c=c;
    }

    public int compareTo(Data t){
        return a-t.a;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();

        Data[] datas=new Data[n];
        for(int i=0;i<n;i++)
            datas[i]=new Data(sc.nextInt(),sc.nextDouble(),sc.next());

        Arrays.sort(datas);

        for(Data data:datas)
            System.out.printf("%d %.2f %s\n",data.a,data.b,data.c);
    }
}


posted @ 2024-02-20 00:17  JiajiaMing  阅读(7)  评论(0编辑  收藏  举报