ITPub博客

首页 > Linux操作系统 > Linux操作系统 > java面向對象(三)

java面向對象(三)

原创 Linux操作系统 作者:xiangying816 时间:2010-06-02 16:54:04 0 删除 编辑
1.面向對象
  |-OOA面向對象的分析
  |-OOD面向對象設計
  |-OOP面向對象程序
2.
對象的產生
  對象聲明:類名稱 對象名稱=null;//在棧中開闢空間
  對象實例化:對象名稱=new 類名稱();//在堆中開闢空間
   在堆中其實放的是對象的各個屬性,因為對象中只能依靠屬性不同來區分。
3.如果只聲明對象不實例化對象就會報空指針異常
4.而全部的方法都保存在全局代碼中
5.一個棧內存只能指向一個堆內存的。
6.一個類至少有一個構造方法,如果沒寫,系統會生成一個無參構造方法,構造方法在new的時候實例化。構造方法是無返回值的,但可以用return 退出構造方法
class Student{
 private String name ;
 private int age ;
 private float english ;
 private float computer ;
 private float math ;
 public Student(){}
 public Student(String n,int a,float e,float c,float m){
  this.setName(n) ;
  this.setAge(a) ;
  this.setEnglish(e) ;
  this.setComputer(c) ;
  this.setMath(m) ;
 }
 public float sum(){
  return english + computer + math ;
 }
 public float avg(){
  return this.sum() / 3 ;
 }
 public float max(){
  float max = computer>math?computer:math ;
  max = max>english?max:english ;
  return max ;
 }
 public float min(){
  float min = computer  min = min  return min ;
 }
 public String getInfo(){
  return "個人信息\n" +
    "\t|- 姓名" + this.getName() + "\n" +
    "\t|- 年齡" + this.getAge() + "\n" +
    "\t|- 數學成績" + this.getMath() + "\n" +
    "\t|- 英語成績" + this.getEnglish() + "\n" +
    "\t|- 計算機成績" + this.getComputer() ;
 }
 public void setName(String n){
  name = n ;
 }
 public void setAge(int a){
  age = a ;
 }
 public void setEnglish(float e){
  english = e ;
 }
 public void setComputer(float c){
  computer = c ;
 }
 public void setMath(float m){
  math = m ;
 }
 public String getName(){
  return name ;
 }
 public int getAge(){
  return age ;
 }
 public float getEnglish(){
  return english ;
 }
 public float getComputer(){
  return computer ;
 }
 public float getMath(){
  return math ;
 }
}
public class ExecDemo{
 public static void main(String args[]){
  Student stu = new Student("張三",30,89.0f,91.0f,87.0f) ;
  System.out.println("總分" + stu.sum()) ;
  System.out.println("平均分" + stu.avg()) ;
  System.out.println("最高分" + stu.max()) ;
  System.out.println("最低分" + stu.min()) ;
  System.out.println(stu.getInfo()) ;
 }
};

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15127599/viewspace-664342/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: java封裝(四)
下一篇: java String (五)
请登录后发表评论 登录
全部评论

注册时间:2011-03-02

  • 博文量
    90
  • 访问量
    187374