> For the complete documentation index, see [llms.txt](https://yubin551.gitbook.io/java-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yubin551.gitbook.io/java-note/basic-object-oriented/accessobjectfieldmethod.md).

# 存取物件的欄位、方法

> 我們知道物件有屬性有方法，但該如何存取及使用呢？

透過『點』運算子(dot operator)，就可以存取到物件的屬性、呼叫物件的方法。

```java
class Human{  //習慣上類別名稱都會字首大寫
  String name;
  int age;
  int height;
  void eat(){
    System.out.println("eating");
  }
  void sleep(){
    System.out.println("Zzz");
  }
}//end of class Human
```

上面是一個名叫Human的class，

定義的屬性有：字串(String)型態的姓名(name)、整數(int)型態的年齡(age)、身高(height)，

定義的方法有：eat()會印出"eating"、sleep()會印出"Zzz"兩個方法。

現在在一個我們自己測試用的類別中，利用Human類別產生物件。

```java
class Test{
  public static void main(String[] args){
    Human h1 = new Human();
    h1.name = "小木";
    h1.age = 22;
    h1.height = 178;
    System.out.println( h1.name );
    h1.eat();
  }
}//end of class Test
```

執行結果：

```
小木
eating
```

沒錯，就是透過 『 . 』去作存取呼叫。

但要記住，不是所有屬性方法都可以呼叫的到，物件導向有一個重要特性叫作『封裝(Encapsulation)』，設計該類別的工程師不會希望你可以碰到所有的資訊，他只會提供他想公開給你的屬性或方法，只有那些方法才能用點運算子存取到，詳細說明會在『修飾子』的部份介紹。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yubin551.gitbook.io/java-note/basic-object-oriented/accessobjectfieldmethod.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
