1、类和对象
2、引用的内存分配
3、封装性
4、构造方法
5、String类
6、this关键字
2、 如何去分析题目
类名 |
属性 |
方法 |
权限 属性名称:类型
-name:String ==> private String name ;
权限 方法名称(参数名称:参数类型,参数名称:参数类型):返回值类型
+setName(name:String):void ==> public void setName(String name)
private String name;
private int age;
private float score;
//修改程序,加入一个构造
public Student(){}
public Student(String name,int age,float score){
this.setName(name) ;
this.setAge(age) ;
this.setScore(score) ;
}
public void setName(String name) {
this.name = name ;
}
public String getName() {
return this.name ;
}
public void setAge(int age) {
this.age = age ;
}
public int getAge() {
return this.age ;
}
public void setScore(float score) {
this.score = score ;
}
public float getScore() {
return this.score ;
}
}
class Class_ {
private String name;
private String de.ion;
public Class_(){}
public Class_(String name,String de.ion){
this.setName(name) ;
this.setDe.ion(de.ion) ;
}
public void setName(String name) {
this.name = name ;
}
public String getName() {
return this.name ;
}
public void setDe.ion(String de.ion) {
this.de.ion = de.ion ;
}
public String getDe.ion() {
return this.de.ion ;
}
}
class Out {
public void print(Student stu, Class_ cla) {
System.out.println("学生姓名:"+stu.getName()) ;
System.out.println("学生年龄:"+stu.getAge()) ;
System.out.println("学生成绩:"+stu.getScore()) ;
System.out.println("班级名称:"+cla.getName()) ;
System.out.println("班级描述:"+cla.getDe.ion()) ;
}
}
public class Demo02
{
public static void main(String args[]){
Student s = new Student() ;
s.setName("王乾") ;
s.setAge(27) ;
s.setScore(99.9f) ;
Class_ c = new Class_() ;
c.setName("JAVA强化班") ;
c.setDe.ion("学习JAVA技术强化班") ;
new Out().print(s,c) ;
}
}
1、定义好要使用的类
2、定义好类中的属性
3、为属性封装
4、为封装后的属性设置setter和getter方法
5、如果需要可以为类中添加构造方法
6、为类中添加所需要的其他方法
*/
class Student
{
//学生编号
private String stuno ;
//学生姓名
private String name ;
//数学成绩
private float mathScore ;
//英语成绩
private float englishScore ;
//计算机成绩
private float computerScore ;
public Student(){}
public Student(String stuno,String name,float mathScore,float englishScore,float computerScore){
this.setStuno(stuno) ;
this.setName(name) ;
this.setMathScore(mathScore) ;
this.setEnglishScore(englishScore) ;
this.setComputerScore(computerScore) ;
}
public void setStuno(String stuno){
this.stuno = stuno ;
}
public void setName(String name){
this.name = name ;
}
public void setMathScore(float mathScore)
{
this.mathScore = mathScore ;
}
public void setEnglishScore(float englishScore)
{
this.englishScore = englishScore ;
}
public void setComputerScore(float computerScore)
{
this.computerScore = computerScore ;
}
public String getStuno(){
return this.stuno ;
}
public String getName(){
return this.name ;
}
public float getMathScore(){
return this.mathScore ;
}
public float getEnglishScore(){
return this.englishScore ;
}
public float getComputerScore(){
return this.computerScore ;
}
//添加所需要的其他方法
// 在编写类的时候一定要记住一个原则:任何的输出绝对不能在类中完成
// 所有要输出的内容在类中只是把值返回来,交给调用处输出
// 取得全部成绩的总和
public float getSum(){
return this.mathScore+this.englishScore+this.computerScore ;
}
//取得平均成绩
public float getAvg(){
return this.getSum() / 3 ;
}
//取最高的成绩
public float getMax(){
//三个成绩相比
float max = 0.0f ;
if (this.mathScore>this.englishScore)
{
max = this.mathScore ;
}
else
{
max = this.englishScore ;
}
if (max<this.computerScore)
{
max = this.computerScore ;
}
return max ;
}
//取最低的成绩
public float getMin(){
//三个成绩相比
float min = 0.0f ;
if (this.mathScore>this.englishScore)
{
min = this.englishScore ;
}
else
{
min = this.mathScore ;
}
if (min>this.computerScore)
{
min = this.computerScore ;
}
return min ;
}
}
public class Demo03{
public static void main(String args[]){
Student s = new Student() ;
s.setStuno("s01") ;
s.setName("王乾") ;
s.setMathScore(89.8f) ;
s.setEnglishScore(99.0f) ;
s.setComputerScore(88.8f) ;
System.out.println(s.getName()+"的成绩单("+s.getStuno()+"):") ;
System.out.println("\t|- 总成绩:"+s.getSum()) ;
System.out.println("\t|- 平均成绩:"+s.getAvg()) ;
System.out.println("\t|- 最高成绩:"+s.getMax()) ;
System.out.println("\t|- 最低成绩:"+s.getMin()) ;
}
};
1、可以画一些类的草图
2、之后定义每个类中的属性,属性定义完之后必须封装
3、封装的属性如果需要被外部所访问,则肯定必须写setter和getter
4、如果需要在对象实例化时可以为其属性赋初始值,则可以加入构造方法,如果有必要则可以加入多个构造(一个是无参的,其他的是有参的)
5、为类中添加程序所必须的方法
6、进行测试
开发中main 方法就是一个测试端,只在里面写少量的代码即可,不可多写,也可以把main 方法称为客户端。
总结
通过这一季的习题练习哈,我们一定要培养面向对象的解决的思路起来哈~~~
刘鹏程lpc 2014-11-16
向着阳光跑 2013-05-17
adam1103 2012-02-17
未知 2012-01-12
圆圆圈圈 2011-05-15
100颗星星 2009-01-03