JAVA:建立類和對象

來源:互聯網
上載者:User

標籤:構造   dna   ble   height   ==   double   ...   建立對象   類型   

package duixiang;

public class duixiang {
/*
* 類的執行個體化:建立對象
*/
public static void main(String[] args) {
  //使用類:建立對象
  //文法:資料類型(類名) 對象名 =new 構造方法;
  Person person = new Person();
  //類的屬性要通過對象進行取值
  //對象的使用: . 對象名.屬性名稱 對象名.方法名
  //調用屬性
  person.name = "張三";
  person.age = 23;
  person.weight = 70.0;
  person.height = 175.0;
  System.out.println("名字為:"+person.name);
  System.out.println("年齡為:"+person.age);
  System.out.println("體重為:"+person.weight);
  System.out.println("身高為:"+person.height);
//調用方法
  person.eat();
  person.eat("米飯");
  person.sleep();
  person.study();
  System.out.println(person.getName("李四"));
  person.setSex(‘男‘);
  System.out.println(person.sex);

}
}
/*
*
* 定義一個人類
*/
class Person{
  //屬性部分(靜態部分)==》成員變數
  public String name;
  public int age;
  public double height;
  public double weight;
  public char sex;

  //行為部分(動態部分)==》方法
  public void study(){
  System.out.println("我正在學習....");
  }

  public void eat() {
  System.out.println("我正在吃飯...");
  }
  public void eat(String foodName){
  System.out.println("我正在吃:"+foodName);
  }
  public void sleep(){
  System.out.println("我正在睡覺...");
  }
  public String getName(String name){
  return name;
  }
  public void setSex(char s){
  sex = s;
  }
}

JAVA:建立類和對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.