Java:类的继承
1
2
//----------------------------------
3
//我的第三个Java程序
4
//关于类的继承
5
//----------------------------------
6![]()
7
//----------------------------------
8
//成绩类
9
//BEGIN-----------------------------
10
class Score
11
{
12
private float Chinese,English,Maths;
13
14
//构造函数,初始化数据
15
Score(float Chinese,float English,float Maths)
16
{
17
this.Chinese=Chinese;
18
this.English=English;
19
this.Maths=Maths;
20
}
21
//返回各科成绩
22
public float GetChinese()
23
{
24
return this.Chinese;
25
}
26
public float GetEnglish()
27
{
28
return this.English;
29
}
30
public float GetMaths()
31
{
32
return this.Maths;
33
}
34
}
35
//END-------------------------------
36![]()
37
//----------------------------------
38
//成绩表类,由Score类继承
39
//关键字 extends
40
//BEGIN-----------------------------
41
class ScoreTable extends Score
42
{
43
//构造函数,初始化数据
44
ScoreTable(float Chinese,float English,float Maths)
45
{
46
super(Chinese,English,Maths);
47
}
48
//计算总分
49
public float Cal_Sum()
50
{
51
return this.GetChinese()+this.GetEnglish()+this.GetMaths();
52
}
53
//计算平均
54
public float Cal_Ave()
55
{
56
return this.Cal_Sum()/3;
57
}
58
}
59
//END-------------------------------
60
public class ThirdJava
61
{
62
public static void main (String[] args)
63
{
64
ScoreTable ST1=new ScoreTable(92,86,90);
65
System.out.println("语文:"+ST1.GetChinese()+"\t英语:"+ST1.GetEnglish()+"\t数学:"+ST1.GetMaths()+"\n");
66
System.out.println("总分:"+ST1.Cal_Sum()+"\t平均分:"+ST1.Cal_Ave());
67
}
68
}
69![]()
2
//----------------------------------3
//我的第三个Java程序4
//关于类的继承5
//----------------------------------6

7
//----------------------------------8
//成绩类9
//BEGIN-----------------------------10
class Score11
{12
private float Chinese,English,Maths;13
14
//构造函数,初始化数据15
Score(float Chinese,float English,float Maths)16
{17
this.Chinese=Chinese;18
this.English=English;19
this.Maths=Maths;20
}21
//返回各科成绩22
public float GetChinese()23
{24
return this.Chinese;25
}26
public float GetEnglish()27
{28
return this.English;29
}30
public float GetMaths()31
{32
return this.Maths;33
}34
}35
//END-------------------------------36

37
//----------------------------------38
//成绩表类,由Score类继承39
//关键字 extends40
//BEGIN-----------------------------41
class ScoreTable extends Score42
{43
//构造函数,初始化数据44
ScoreTable(float Chinese,float English,float Maths)45
{46
super(Chinese,English,Maths);47
}48
//计算总分49
public float Cal_Sum()50
{51
return this.GetChinese()+this.GetEnglish()+this.GetMaths();52
}53
//计算平均54
public float Cal_Ave()55
{56
return this.Cal_Sum()/3;57
}58
}59
//END-------------------------------60
public class ThirdJava61
{62
public static void main (String[] args)63
{64
ScoreTable ST1=new ScoreTable(92,86,90);65
System.out.println("语文:"+ST1.GetChinese()+"\t英语:"+ST1.GetEnglish()+"\t数学:"+ST1.GetMaths()+"\n");66
System.out.println("总分:"+ST1.Cal_Sum()+"\t平均分:"+ST1.Cal_Ave());67
}68
}69



浙公网安备 33010602011771号