類和對象

來源:互聯網
上載者:User

標籤:microsoft   color   main   family   關係   使用   ros   事物   語言   

1.概念:萬物皆對象。

2.類和對象的關係:類是對象的抽象(類型),對象是類的執行個體。

3.對象具有的兩個特徵:屬性:對象具有的特徵。(例如:姓名;性別;)

                      方法:對象執行的操作。(例如:吃,喝......)

4.對象:用來描述客觀事物的一個實體,由一組屬性和方法構成。(例如:長城是名勝古迹)

5.封裝:將屬性和方法封裝到對象當中。

6.好處:隱藏其內部細節,不展露外部細節。

7.Java中類名的規範:(1)類的首字母大寫。

          (2)類名不能使用Java關鍵字。

          (3)除了"_"與"$"符號以外,不能包含其他字元。

          (4)不要包含數字以外以及中文漢字。

8.類是class的對象,所有的類都是Object的子類。

9.Java是一門物件導向的語言。

10.類的定義:存取修飾詞 class 類名{

             存取修飾詞 資料類型 屬性名稱;

             存取修飾詞 方法傳回值類型 方法名(){

         }

11.類中屬性和方法的調用:使用對象名"."進行調用(確保已經建立對象)

例如:Student stu=new Student();

      stu.name="張三";

      stu.showStudent();

         

public class Student{           String name;   //姓名           int  age;      //年齡           String classNo;  //班級           String hobby;   //愛好
//輸出資訊方法 public void show(){ System.out.println(name+"\n年齡:"+age+"\n就讀於:"+classNo+"\n愛好:"+hobby) }}
//輸出學員資訊
public class InitialStudent{ public static void main (String args [] ) Student student=new Student(); //建立對象 student.name="張浩"; //給每個屬性賦值 student.age=10; student.classNo="S1班"; student.hobby="籃球"; student.show(); //調用方法 }}


遊人類:import java.util.Scanner;public class Visitor{         String name;  //姓名         int age;    //年齡    public void shoe() {      Scanner input=new Scanner(System.in);      while(!"n".equals(name)){         if(age>=18&&age<=60){    //判斷年齡          System.out.println(name+"的年齡為:"+age+"門票價格為:20元\n")     }else{         System.out.println(name+"的年齡為:"+age+"門票免費\n")  }      System.out.println("請輸入姓名:");      name=input.next();        //給name屬性賦值   if(!"n".equals(name)){   System.out.print("請輸入年齡:");   age=input.nextInt();     //給age屬性賦值  }}System.out.print("退出程式") }}

 

 

//輸出門票資訊
import java.util.Scanner;public class InitialVistor{ public static void main(String [] args){ Scanner input=new Scanner(System.in); Visitor v=new Visitor(); System.out.print("請輸入姓名:"); v.name=input.next(); System.out.print("請輸入年齡:"); v.age=input.nextInt(); v.show(); }}

 

 

 

 

類和對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.