Java面向对象思想

类(class)

类的修饰符:

-- public:公共类,无任何限制

-- static:抽象类,无法被实例化

-- final :无法被继承


类成员的访问修饰符:

-- default:被该类自身、同一个包中其他类

-- public:可以被所有类访问

-- private:只能被该类自身访问

-- protected:被该类自身、同个包中其他类、子类访问

-- private protected:被该类自身、子类访问

类成员的非访问修饰符:

-- static:静态变量,独立于类,存在于整个程序运行的生命周期,可以在类未被实例化时访问

-- final:值无法被修改

-- volatile:可以被多个线程同时修改

1)类的基本使用

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
//以学生类为例
class Student{
//私有变量
private String name, number;
private double MathScore, ChineseScore, EnglishScore;
//公有函数
public void PrintMessage(){
System.out.println("Name : " + name);
System.out.println("StuNumber : " + number);
System.out.println("average score : " +
(MathScore + ChineseScore + EnglishScore)/3);
}
//构造函数
Student(final String s1,final String s2,
final double score1,final double score2,final double score3){
this.name = s1;
this.number = s2;
this.MathScore = score1;
this.ChineseScore = score2;
this.EnglishScore = score3;
}
}
class Main{
public static void main(String args[]){
Student s = new Student("peter","20070803",83,90,60);
s.PrintMessage();
}
}

2)静态变量/方法(static)

我们可以使用静态变量,继续完善1) 中的学生类。我们建立一个total_num的静态变量,该变量属于整个学生群体。可以被实例化的类访问,也可以直接访问。

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
class Student{
private String name, number;
private double MathScore, ChineseScore, EnglishScore;
static int total_num = 0;
public void PrintMessage(){
System.out.println("Name : " + name);
System.out.println("StuNumber : " + number);
System.out.println("average score : " + (MathScore + ChineseScore + EnglishScore)/3);
System.out.println("total_num : " + total_num);
}
Student(final String s1,final String s2,final double score1,final double score2,final double score3){
this.name = s1;
this.number = s2;
this.MathScore = score1;
this.ChineseScore = score2;
this.EnglishScore = score3;
total_num ++;
}
}

class Main{
public static void main(String args[]){
Student s1 = new Student("Peter","20070803",83,90,60);
Student s2 = new Student("Jackson","20140225",80,100,78);
Student s3 = new Student("Lisa","20021208",99,67,89);
s3.PrintMessage();
System.out.println(Student.total_num);
}
}
//Name : Lisa
//StuNumber : 20021208
//average score : 85.0
//total_num : 3
//3

3)抽象类(abstract)

抽象类是含有抽象方法的类,使用abstract声明。

抽象类强调从属关系,接口强调特定功能的实现。继承抽象类 和 实现接口 的类都必须描述所有未实现的功能。

抽象类与接口的具体区别:

A.接口只能有未实现的方法。而抽象类既能拥有普通的方法,又有抽象的方法,所以既能自己完成一些功能,又给子类提供可能性。

B.一个类可以实现多个接口,但只能继承一个抽象类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
abstract class Employee{
final int BasicSalory = 2000;
abstract void salory();
}
class Manager extends Employee{
void salory(){System.out.println("Salory is " + 5*BasicSalory);}
}

class Worker extends Employee{
void salory(){System.out.println("Salory is " + 3*BasicSalory);}
}

class Main{
public static void main(String []args){
// Employee t = new Employee(); //java: Employee是抽象的; 无法实例化
// t.salory();
Worker a = new Worker();
a.salory();
Manager b = new Manager();
b.salory();
}
}

继承(extends)

Java使用extends实现单一继承(在java中多重继承是不被允许的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class shape{
void draw(){System.out.println("shape drawed!");};
void erase(){System.out.println("shape erased!");};
}
class cricle extends shape{
void draw(){System.out.println("cricle drawed!");}
void erase(){System.out.println("cricle earsed!");}
}

class Main{
public static void main(String arg[]){
shape s = new shape();
s.draw();s.erase();
s = new cricle();
s.draw();s.erase();
}
}
//shape drawed!
//shape erased!
//cricle drawed!
//cricle earsed!

包(package)

"包" 是 java项中的用于存放各种类的文件夹。可以使用import语句引用或者直接引用。包的引用形式和文件夹路径是强联系的。

比如:我在src中创建一个shape文件夹,里面存放一个cricle类

1
2
3
4
5
6
7
8
9
package shape; //使用package语句,声明在哪个包中
public class cricle {
public static void draw(){
System.out.println("cricle drawed!");
}
public static void erase(){
System.out.println("cricle erased!");
}
}

此时类的路径为“shape/cricle.class”,所以在主程序中使用shape.cricle来引用

1
2
3
4
5
6
7
8
9
10
import shape.cricle;//也可以使用shape.cricle来引用
class Main{
public static void main(String arg[]) {
cricle s = new cricle();
s.draw();
s.erase();
}
}
//cricle drawed!
//cricle earsed!

接口(interface)

在java中,接口是抽象方法的集合。

接口的声明方法和类很相似,但接口只声明要实现的方法,和定义常量,不描述具体的属性和方法。

接口无法被实例化,但可以被类实现(implements)。一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface MyInterface{
public void add(int x,int y);
public void volume(int x,int y,int z);
}
class Demo implements MyInterface{
public void add(int x,int y){System.out.println("Add result: "+(x+y));}
public void volume(int x,int y,int z){System.out.println("Volume result: "+(x*y*z));}
}
class Main{
public static void main(String args[]){
Demo d=new Demo();
d.add(10,20);
d.volume(10,10,10);
}
}
//Add result: 30
//Volume result: 1000

Java面向对象思想
https://czwcugb.github.io/其他/Java面向对象思想/
作者
ChenZhiwei
发布于
2025年1月14日
许可协议