记录一些个人的理解,可能不太对
1.类与对象
类:好比String
对象(实例):eg:String str1 此时的str1就是String类的对象或者实例;
package:可以理解为文件下的路径,比如com.xyz
1.1源文件声明规则
一个源文件中只能有一个public类。
一个源文件可以有多个非public类。
源文件的名称应该和public类的类名保持一致。
每个源文件中,先写package语句,再写import语句,最后定义类。
1.2类的定义
public: 所有对象均可以访问
private: 只有本类内部可以访问
protected:同一个包或者子类中可以访问
不添加修饰符:在同一个包中可以访问
package com.czl;
import java.util.Scanner;
class Student{ //一个源文件中只能有一个public类。//一个源文件可以有多个非public类。
public int x;
private int y;
//public 在class外也能访问
//private 在class里面能访问,外面访问不了
}
public class Main {
public static void main(String[] args) throws Exception{
Student student = new Student();
student.x=3; //不报错
//student.y=4; //报错
}
}
静态(带static修饰符)成员变量/函数与普通成员变量/函数的区别:
- 所有static成员变量/函数在类中只有一份,被所有类的对象共享;
- 所有普通成员变量/函数在类的每个对象中都有独立的一份;
- 静态函数中只能调用静态函数/变量;普通函数中既可以调用普通函数/变量,也可以调用静态函数/变量。
//静态变量与普通变量的区别
package com.czl;
import java.util.Scanner;
class Student{ //一个源文件中只能有一个public类。//一个源文件可以有多个非public类。
public int y=-1; //public 在class外也能访问
public static int x=-1; //static 所以这个类的对象共享的变量
}
public class Main {
public static void main(String[] args) throws Exception{
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
//s1,s2,s3访问一个共同的x(相当于教室里面的黑板),但他们都有一个自己单独的y(相当于每个人手里拿着自己的草稿纸)
Student.x=2; //访问静态变量通过类访问
System.out.printf("%d %d %d\n",student1.x,student2.x,student3.x); //结果是2 2 2
student1.y=2;
System.out.printf("%d %d %d\n",student1.y,student2.y,student3.y); //结果是 2 -1 -1
}
}
关系:
//Point包
/
package com.czl.point;
//类名最好大写
public class Point {
private int x;
private int y;
//下面相当于封装,xy是私有变量,外面函数要访问xy,要么通过set函数赋值,要么通过get函数取值
//构造函数,初始化对象时候可以传一些参数
public Point(int x, int y) {
this.x = x; //this 访问当前的成员变量
this.y = y;
}
//表示给x赋值
public void setX(int x) {
this.x = x;
}
//表示给y赋值
public void setY(int y) {
this.y = y;
}
//返回x
public int getX() {
return x;
}
//返回y
public int getY() {
return y;
}
//把当前的点转换成字符串
public String toString() {
return String.format("(%d, %d)", x, y);
}
}
package com.czl;
import com.czl.point.Point;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
//Alt+Enter
Point point = new Point(3,4);
System.out.println(point.toString());
}
}
同理:
普通函数:每个对象自己有的
静态(共有)函数:所有对象共有的
上述文件结构:
1.3继承和多态
每个类只能继承一个类。为什么会有这个东西:好比LOL每个英雄都有血条,一个个定义多累啊,所以继承一个共有的函数
文件结构:
//ColorPoint
//Point没变化
package com.czl.point;
public class ColorPoint extends Point{ //继承Point类
public 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() { //子类toString,优先使用
//多态 同一个类的实例,调用相同的函数,运行结果不同
return String.format("(%d, %d, %s)", super.getX(), super.getY(), this.color);
}
}
//Main
package com.czl;
import com.czl.point.ColorPoint;
import com.czl.point.Point;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
//Alt+Enter
Point point1 = new Point(3,4);
Point point2 = new ColorPoint(3,4,"red"); //子类可以用父类来存
// 多态,同一个类的实例,调用相同的函数,运行结果不同
System.out.println(point1.toString()); //(3, 4)
System.out.println(point2.toString()); //(3, 4, red)
}
}
2.接口
interface与class类似。主要用来定义类中所需包含的函数。
接口也可以继承其他接口,一个类可以实现多个接口。
2.1接口的定义
public interface Role {
public void greet();
public void move();
public int getSpeed(); //移动速度
}
2.2接口的继承
每个接口可以继承多个接口
public 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[] heros = {new Zeus(), new Athena()};
for (Hero hero: heros) {
hero.greet();
}
}
}
3.关于接口的总结:
//Mian函数
package com.czl;
import com.czl.role.Hero;
import com.czl.role.imply.Athena;
import com.czl.role.imply.Zeus;
//Alt+Enter导入包
public class Main {
public static void main(String[] args) throws Exception{
Hero[] heros = {new Zeus(), new Athena()};
for (Hero hero: heros) {
hero.greet();
}
}
}
//Athena类
package com.czl.role.imply;
import com.czl.role.Hero;
public class Athena implements Hero{
//Alt+Insert 之后点击实现方法
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 20;
}
}
//Zeus类
package com.czl.role.imply;
import com.czl.role.Hero;
public 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;
}
}
//Role接口
package com.czl.role;
public interface Role {
public void greet();
public void move();
public int getSpeed(); //移动速度
}
//Hero接口
package com.czl.role;
//继承
public interface Hero extends Role{ //public interface Hero extends Role 继承Role接口
public void attack();
}