Skip to content · ⁨ข้ามไปยังเนื้อหา⁩

Class Creation · ⁨การสร้างคลาส⁩

AP Computer Science A · Topic 3 · ⁨หัวข้อ 3⁩

Video lesson for this topic · ⁨บทเรียนวิดีโอสำหรับหัวข้อนี้⁩ Open the video page · ⁨เปิดหน้าวิดีโอ⁩
7:26

การสร้างคลาส

นี่คือนักเรียน ซึ่งจัดเก็บเป็นตัวแปรอิสระสามตัว: ชื่อ, ปีการศึกษา, และเกรด. นั่นทำงานได้ดี.ตอนนี้เพิ่มนักเรียนอีกพันคน. คุณจำเป็นต้องใช้ตัวแปรสามพันตัว…

English narration · English + 中文 subtitles burned in · ⁨การบรรยายภาษาอังกฤษ · คำบรรยายภาษาอังกฤษ + 中文 ลอยตัวบนภาพ⁩

3.1

Abstraction and Program Design · ⁨การนามธรรมและการออกแบบโปรแกรม⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.1.A: Represent the design of a program by using natural language or creating diagrams that indicate the classes in the program and the data and procedural abstractions found in each class by including all attributes and behaviors.

  • 3.1.A.1 Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand and bringing together related and useful details, abstraction reduces complexity and allows one to focus on the idea.
  • 3.1.A.2 Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Data abstraction manages complexity by giving data a name without referencing the specific details of the representation. Data can take the form of a single variable or a collection of data, such as in a class or a set of data.
  • 3.1.A.3 An attribute is a type of data abstraction that is defined in a class outside any method or constructor. An instance variable is an attribute whose value is unique to each instance of the class. A class variable is an attribute shared by all instances of the class.
  • 3.1.A.4 Procedural abstraction provides a name for a process and allows a method to be used only knowing what it does, not how it does it. Through method decomposition, a programmer breaks down larger behaviors of the class into smaller behaviors by creating methods to represent each individual smaller behavior. A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for code reuse, which helps manage complexity.
  • 3.1.A.5 Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
  • 3.1.A.6 Using procedural abstraction in a program allows programmers to change the internals of a method (to make it faster, more efficient, use less storage, etc.) without needing to notify method users of the change as long as the method signature and what the method does is preserved.
  • 3.1.A.7 Prior to implementing a class, it is helpful to take time to design each class including its attributes and behaviors. This design can be represented using natural language or diagrams.
ไทย

วัตถุประสงค์การเรียนรู้ 3.1.A: แสดงการออกแบบโปรแกรมโดยใช้ภาษาธรรมชาติหรือสร้างแผนภาพที่ระบุคลาสในโปรแกรมและ抽象ข้อมูลและกระบวนการที่พบในแต่ละคลาสโดยรวมถึงคุณสมบัติและการกระทำทั้งหมด

  • 3.1.A.1 Abstraction คือกระบวนการลดความซับซ้อนด้วยการมุ่งเน้นไปที่แนวคิดหลัก通过将ไม่เกี่ยวข้องกับคำถามปัจจุบันซ่อนไว้และรวบรวมรายละเอียดที่เกี่ยวข้องและมีประโยชน์เข้าด้วยกัน Abstraction ช่วยลดความซับซ้อนและทำให้สามารถมุ่งเน้นไปที่แนวคิดได้
  • 3.1.A.2 Data abstraction ให้การแยกระหว่างคุณสมบัติเชิงนามธรรมของชนิดข้อมูล รายละเอียดจริงของการแสดงผล Data abstraction จัดการความซับซ้อนโดยให้ชื่อแก่ข้อมูลโดยไม่อ้างอิงรายละเอียดเฉพาะของการแสดงผล ข้อมูลอาจอยู่ในรูปแบบของตัวแปรเดียวหรือกลุ่มข้อมูล เช่น ในคลาสหรือชุดข้อมูล
  • 3.1.A.3 Attribute เป็นประเภทหนึ่งของ Data abstraction ที่กำหนดไว้ในคลาสภายนอก-method หรือ constructor Instance variable เป็น attribute ที่มีค่าเฉพาะสำหรับแต่ละ instance ของคลาส Class variable เป็น attribute ที่แบ่งปันโดยทุก instance ของคลาส
  • 3.1.A.4 Procedural abstraction ให้ชื่อสำหรับกระบวนการและทำให้สามารถใช้ method ได้โดยรู้เพียงว่าทำอะไร ไม่ใช่วิธีการทำ ผ่าน method decomposition นักเขียนโปรแกรมจะย่อยพฤติกรรมขนาดใหญ่ของคลาสออกเป็นพฤติกรรมย่อยๆ โดยการสร้าง methods เพื่อแสดงพฤติกรรมย่อยแต่ละอย่าง Procedural abstraction อาจดึงคุณลักษณะที่ร่วมกันมาเพื่อทำให้ฟังก์ชัน性工作ทั่วไปแทนที่จะทำซ้ำโค้ด สิ่งนี้ช่วยให้ใช้โค้ดซ้ำได้ ซึ่งช่วยจัดการความซับซ้อน
  • 3.1.A.5 การใช้ parameters ช่วยให้ procedures可以被 generalize ทำให้ procedures สามารถถูก reuse ด้วยช่วงของ input values หรือ arguments
  • 3.1.A.6 การใช้ procedural abstraction ในโปรแกรมอนุญาตให้นักเขียนโปรแกรมเปลี่ยนแปลงภายในของ method (เพื่อให้เร็วขึ้น มีประสิทธิภาพมากขึ้น ใช้พื้นที่จัดเก็บน้อยลง ฯลฯ) โดยไม่ต้องแจ้ง给用户关于การเปลี่ยนแปลงตราบใดที่ method signature และสิ่งที่ method ทำยังคงอยู่
  • 3.1.A.7 ก่อนที่จะดำเนินการสร้างคลาส จะเป็นการดีที่จะใช้เวลาในการออกแบบแต่ละคลาส รวมถึง properties และ behaviors การออกแบบนี้สามารถแสดงได้ด้วยภาษาธรรมชาติหรือแผนภาพ

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Abstraction 抽象 means hiding detail behind a simple interface – you use a String without knowing how it stores characters. Good design breaks a problem into classes, each responsible for one idea. This topic is about writing your own classes.

ไทย
จิ๊กซอว์กำลังประกอบ: คลาสและเมทόδเป็นชิ้นส่วนแยกส่วนของการออกแบบโปรแกรมที่ใหญ่ขึ้น
จิ๊กซอว์กำลังประกอบ: คลาสและเมทόδเป็นชิ้นส่วนแยกส่วนของการออกแบบโปรแกรมที่ใหญ่ขึ้น

นามธรรม หมายถึงการซ่อนรายละเอียดไว้เบื้องหลังอินเทอร์เฟซที่เรียบง่าย – คุณใช้ String โดยไม่ต้องรู้วิธีการจัดเก็บข้อมูล ตัวอักษร การออกแบบที่ดีจะแบ่งปัญหาออกเป็นคลาส แต่ละคลาสรับผิดชอบแนวคิดหนึ่งประเด็น หัวขอนี้เกี่ยวกับการเขียน คลาสของคุณเอง

การย่อยสลายโปรแกรมออกเป็นโมดูลและซับโมดูล
การแยกย่อยโปรแกรมออกเป็นโมดูลและ submodule
3.2

The Impact of Program Design · ⁨ผลกระทบของการออกแบบโปรแกรม⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.2.A: Explain the social and ethical implications of computing systems.

  • 3.2.A.1 System reliability refers to the program being able to perform its tasks as expected under stated conditions without failure. Programmers should make an effort to maximize system reliability by testing the program with a variety of conditions.
  • 3.2.A.2 The creation of programs has impacts on society, the economy, and culture. These impacts can be both beneficial and harmful. Programs meant to fill a need or solve a problem can have unintended harmful effects beyond their intended use.
  • 3.2.A.3 Legal issues and intellectual property concerns arise when creating programs. Programmers often reuse code written by others and published as open source and free to use. Incorporation of code that is not published as open source requires the programmer to obtain permission and often purchase the code before integrating it into their program.
ไทย

วัตถุประสงค์การเรียนรู้ 3.2.A: อธิบายผลกระทบทางสังคมและจริยธรรมของระบบคอมพิวเตอร์

  • 3.2.A.1 System reliability หมายถึงความสามารถของโปรแกรมในการดำเนินงานตามหน้าที่ที่กำหนดภายใต้เงื่อนไขที่ระบุไว้โดยไม่เกิดความล้มเหลว นักเขียนโปรแกรมควรพยายามเพิ่ม System reliability ให้มากที่สุดโดยการทดสอบโปรแกรมด้วยสภาวะต่างๆ
  • 3.2.A.2 การสร้างโปรแกรมมีผลกระทบต่อสังคม เศรษฐกิจ และวัฒนธรรม ผลกระทบเหล่านี้สามารถเป็นทั้งประโยชน์และอันตราย โปรแกรมที่ออกแบบมาเพื่อตอบสนองความต้องการหรือแก้ปัญหาอาจก่อให้เกิดผลกระทบทางลบที่ไม่ได้ตั้งใจเหนือกว่าการใช้ตามวัตถุประสงค์
  • 3.2.A.3 ปัญหาทางกฎหมายและข้อกังวลเรื่องทรัพย์สินทางปัญญาเกิดขึ้นเมื่อการสร้างโปรแกรม นักเขียนโปรแกรมมักนำโค้ดที่เขียนโดยผู้อื่นมาใช้ใหม่และเผยแพร่ในรูปแบบ open source ซึ่งใช้งานฟรี การนำโค้ดที่ไม่ได้เผยแพร่ในรูปแบบ open source มาใช้ต้องได้รับอนุญาตจากเจ้าของและมักจะซื้อลิขสิทธิ์ก่อนนำไปรวมในโปรแกรม

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Design choices affect whether code is correct, readable, and reusable. Encapsulation 封装 – keeping data private and exposing it only through methods – protects an object's state from misuse and lets you change the inside without breaking users of the class. Thoughtful naming, single-purpose methods, and testing reduce bugs.

Design also carries responsibility beyond the code. System reliability 系统可靠性 - a program performing its tasks as expected, without failure - is something programmers should maximise through careful design and testing. Programs have real impacts on society, the economy, and culture that can be both beneficial and harmful. And creating programs raises legal and intellectual-property 知识产权 concerns: programmers often reuse code published as open source 开源 and free to use, but must respect its licence and give credit rather than copy others' work as their own.

ไทย

การเลือกการออกแบบมีผลต่อความถูกต้อง ความอ่านง่าย และการนำโค้ดไปใช้ใหม่ การห่อหุ้ม (Encapsulation) – การเก็บข้อมูลให้เป็นส่วนตัวและเปิดเผยผ่านเมทόδเท่านั้น – ช่วยปกป้องสถานะของออบเจกต์จากการใช้งานที่ไม่เหมาะสมและให้คุณสามารถแก้ไขด้านในโดยไม่ทำให้ผู้ใช้คลาสเสียไป การตั้งชื่ออย่างรอบคอบ เมทόδที่มีวัตถุประสงค์เดียว และการทดสอบช่วยลดข้อผิดพลาดได้

การออกแบบยังมีความรับผิดชอบเกินกว่าแค่โค้ด ความน่าเชื่อถือของระบบ – โปรแกรมที่ทำหน้าที่ตาม的预期โดยไม่ล้มเหลว – เป็นสิ่งที่นักพัฒนาควรเพิ่มสูงสุดผ่านการออกแบบและการทดสอบอย่างพิถีพิถัน โปรแกรมมีผลกระทบจริงต่อสังคม เศรษฐกิจ และวัฒนธรรม ซึ่งอาจเป็น ทั้งประโยชน์และอันตราย และการสร้างโปรแกรมยกเรื่อง กฎหมายและทรัพย์สินทางปัญญา ขึ้นมา: นักพัฒนามักนำโค้ดที่เผยแพร่เป็น code แบบ open source มาใช้ใหม่และใช้ฟรี แต่ต้องเคารพใบอนุญาตและให้เครดิตแทนที่จะลอกผลงานของผู้อื่นมาเป็นของตนเอง

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
Abstraction/əbˈstrækʃn/ Abstraction
Encapsulation/ɪnˌkæpsjʊˈleɪʃn/ การห่อหุ้มข้อมูล (Encapsulation)
System reliability/ˈsɪstəm rɪˌlaɪəˈbɪlɪti/ ความน่าเชื่อถือของระบบ
legal and intellectual-property/ˈliːɡl ænd ˌɪntəˈlektʃuːəl ˈprɒpəti/ ด้านกฎหมายและทรัพย์สินทางปัญญา
open source/ˈəʊpən sɔːs/ open source
instance variables/ˈɪnstəns ˈveərɪəblz/ instance variables
3.3

The Anatomy of a Class · ⁨กายวิภาคศาสตร์ของคลาส⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.3.A: Develop code to designate access and visibility constraints to classes, data, constructors, and methods.

  • 3.3.A.1 Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes. The keywords public and private affect the access of classes, data, constructors, and methods. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.
  • 3.3.A.2 In this course, classes are always designated public and are declared with the keyword class.
  • 3.3.A.3 In this course, constructors are always designated public.
  • 3.3.A.4 Instance variables belong to the object, and each object has its own copy of the variable.
  • 3.3.A.5 Access to attributes should be kept internal to the class in order to accomplish encapsulation. Therefore, it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.
  • 3.3.A.6 Access to behaviors can be internal or external to the class. Methods designated as public can be accessed internally or externally to a class, whereas methods designated as private can only be accessed internally to the class.
ไทย

จุดประสงค์การเรียนรู้ 3.3.A: เขียนโค้ดเพื่อกำหนดขอบเขตการเข้าถึงและเงื่อนไขความมองเห็นสำหรับคลาส ข้อมูล ตัวสร้าง (constructors) และเมท็ด

  • 3.3.A.1 การห่อหุ้มข้อมูล (Data encapsulation) เป็นเทคนิคที่ซ่อนรายละเอียดการดำเนินการภายในของคลาสไม่ให้คลาสภายนอกเห็น คีย์เวิร์ด public และ private มีผลต่อการเข้าถึงของคลาส ข้อมูล ตัวสร้าง และเมท็ด คีย์เวิร์ด private จำกัดการเข้าถึงเฉพาะในคลาสผู้ประกาศ ในขณะที่คีย์เวิร์ด public อนุญาตให้เข้าถึงได้จากคลาสอื่นนอกเหนือจากคลาสผู้ประกาศ
  • 3.3.A.2 ในรายวิชานี้ คลาสจะถูกกำหนดให้始终是 public และประกาศด้วยคีย์เวิร์ด class
  • 3.3.A.3 ในรายวิชานี้ ตัวสร้างจะ始终以 public เสมอ
  • 3.3.A.4 ตัวแปรอินสแตนซ์ (Instance variables) เป็นของวัตถุ และแต่ละวัตถุจะมีสำเนาของตัวแปรนั้นเป็นของตัวเอง
  • 3.3.A.5 การเข้าถึงคุณสมบัติควรถูกจำกัดให้อยู่ภายในคลาสเพื่อให้บรรลุหลักการห่อหุ้มข้อมูล ดังนั้นจึงเป็นการปฏิบัติที่ดีในการกำหนดให้ตัวแปรอินสแตนซ์สำหรับคุณสมบัติเหล่านี้เป็น private เว้นแต่สเปกซิกิเคชันของคลาสจะระบุไว้ differently
  • 3.3.A.6 การเข้าถึงพฤติกรรมสามารถเป็นภายในหรือภายนอกคลาส เมท็ดที่กำหนดให้始终是 public สามารถเข้าถึงได้ทั้งภายในและภายนอกคลาส ในขณะที่เมท็ดที่กำหนดให้始终是 private สามารถเข้าถึงได้เฉพาะภายในคลาสเท่านั้น

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

A class has three parts: instance variables 实例变量 (fields – the object's data), constructors (build objects), and methods (behavior). Fields are usually private; methods are usually public:

ไทย

คลาสประกอบด้วยสามส่วน: ตัวแปรinstance (fields) – ข้อมูลของออบเจกต์, คอนสตรัคเตอร์ (constructors) – สร้างออบเจกต์, และ เมทόδ (methods) – พฤติกรรม Fields มักจะเป็น private; Metods มักจะเป็น public:

A class diagram: private attributes and public methods
A class diagram: private attributes and public methods
public class Student {
    private String name;      // instance variable
    private int score;

    public Student(String n, int s) {   // constructor
        name = n;
        score = s;
    }
    public int getScore() { return score; }   // accessor
}
แผนภาพ: คลาสเป็นเทมเพลตที่กำหนดวิธีสร้างออบเจกต์ชนิดนั้น
แผนภาพ: คลาสเป็นเทมเพลตที่กำหนดวิธีสร้างออบเจกต์ชนิดนั้น
Explore · ⁨สำรวจ⁩

See an object's fields as boxes · ⁨เห็นฟิลด์ของ object เป็นกล่อง⁩

A class groups related data (its fields) and methods. Each object gets its own set of field boxes; assigning to one changes that object only. · ⁨class รวมข้อมูลที่เกี่ยวข้อง (fields) และเมทოდเข้าด้วยกัน Each object จะมีชุดกล่องฟิลด์ของตัวเอง; การ assign เข้าไปในกล่องของวัตถุหนึ่ง จะส่งผลต่อวัตถุนั้นเท่านั้น⁩

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
constructor/kənˈstrʌktə/ constructor
accessor (getter)/əkˈsesə/ accessor (getter)
3.4

Constructors · ⁨คอนสตรัคเตอร์⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.4.A: Develop code to declare instance variables for the attributes to be initialized in the body of the constructors of a class.

  • 3.4.A.1 An object's state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This defines a has-a relationship between the object and its instance variables.
  • 3.4.A.2 A constructor is used to set the initial state of an object, which should include initial values for all instance variables. When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.
  • 3.4.A.3 When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object, and methods are prevented from modifying the state of the original object.
  • 3.4.A.4 When no constructor is written, Java provides a no-parameter constructor, and the instance variables are set to default values according to the data type of the attribute. This constructor is called the default constructor.
  • 3.4.A.5 The default value for an attribute of type int is 0. The default value of an attribute of type double is 0.0. The default value of an attribute of type boolean is false. The default value of a reference type is null.
ไทย

จุดประสงค์การเรียนรู้ 3.4.A: เขียนโค้ดเพื่อประกาศตัวแปรอินสแตนซ์สำหรับคุณสมบัติที่จะเริ่มต้นค่าในส่วนของตัวสร้างของคลาส

  • 3.4.A.1 สถานะ (state) ของวัตถุหมายถึงคุณสมบัติและค่าของมัน ณ เวลาหนึ่งๆ และถูกกำหนดโดยตัวแปรอินสแตนซ์ที่เป็นของวัตถุนั้น สิ่งนี้กำหนดความสัมพันธ์แบบ has-a ระหว่างวัตถุกับตัวแปรอินสแตนซ์ของมัน
  • 3.4.A.2 ตัวสร้างใช้เพื่อตั้งค่าเริ่มต้นของวัตถุ ซึ่งควรประกอบด้วยค่าเริ่มต้นสำหรับตัวแปรอินสแตนซ์ทั้งหมด เมื่อมีการเรียกใช้ตัวสร้าง จะมีการจัดสรรหน่วยความจำสำหรับวัตถุและกลับคืนค่าอ้างอิงของวัตถุที่เกี่ยวข้อง พารามิเตอร์ของตัวสร้าง หากมีการระบุ จะให้ข้อมูล用来เริ่มต้นตัวแปรอินสแตนซ์
  • 3.4.A.3 เมื่อวัตถุที่แก้ไขได้ (mutable object) เป็นพารามิเตอร์ของตัวสร้าง ตัวแปรอินสแตนซ์ควรเริ่มต้นด้วยสำเนาของวัตถุที่ถูกอ้างอิง ด้วยวิธีนี้ ตัวแปรอินสแตนซ์จะไม่ถืออ้างอิงไปยังวัตถุต้นฉบับ และป้องกันไม่ให้เมท็ดแก้ไขสถานะของวัตถุต้นฉบับ
  • 3.4.A.4 เมื่อไม่มีการเขียนตัวสร้าง Java จะให้ตัวสร้างที่ไม่มีพารามิเตอร์ และตัวแปรอินสแตนซ์จะถูกตั้งค่าเป็นค่าเริ่มต้นตามชนิดข้อมูลของคุณสมบัติ ตัวสร้างนี้เรียกว่า ตัวสร้างเริ่มต้น (default constructor)
  • 3.4.A.5 ค่าเริ่มต้นของคุณสมบัติที่มีชนิด int คือ 0 ค่าเริ่มต้นของคุณสมบัติที่มีชนิด double คือ 0.0 ค่าเริ่มต้นของคุณสมบัติที่มีชนิด boolean คือ false ค่าเริ่มต้นของชนิดอ้างอิงคือ null

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

A constructor 构造函数 has the same name as the class and no return type. It runs when you write new, and its job is to initialize the fields. A class can have several constructors with different parameter lists (overloading 重载); a no-argument constructor sets defaults.

ไทย

คอนสตรัคเตอร์ มี ชื่อเดียวกับคลาส และไม่มีประเภทข้อมูลกลับคืน มันจะทำงานเมื่อคุณเขียน new และหน้าที่ของมันคือการเริ่มต้นค่า fields คลาสสามารถมีคอนสตรัคเตอร์หลายตัวที่มีรายการพารามิเตอร์ต่างกัน (overloading); คอนสตรัคเตอร์ ไม่มีพารามิเตอร์ จะตั้งค่าเริ่มต้น

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
overloading/ˌəʊvəˈləʊdɪŋ/ การโอเวอร์โหลด (Overloading)
3.5

Methods: How to Write Them · ⁨เมทόδ: วิธีเขียน⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.5.A: Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods.

  • 3.5.A.1 A void method does not return a value. Its header contains the keyword void before the method name.
  • 3.5.A.2 A non-void method returns a single value. Its header includes the return type in place of the keyword void.
  • 3.5.A.3 In non-void methods, a return expression compatible with the return type is evaluated, and the value is returned. This is referred to as return by value.
  • 3.5.A.4 The return keyword is used to return the flow of control to the point where the method or constructor was called. Any code that is sequentially after a return statement will never be executed. Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor.
  • 3.5.A.5 An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables. An accessor method is a non-void method.
  • 3.5.A.6 A mutator (modifier) method is a method that changes the values of the instance variables or class variables. A mutator method is often a void method.
  • 3.5.A.7 Methods with parameters receive values through those parameters and use those values in accomplishing the method's task.
  • 3.5.A.8 When an argument is a primitive value, the parameter is initialized with a copy of that value. Changes to the parameter have no effect on the corresponding argument.
ไทย

จุดประสงค์การเรียนรู้ 3.5.A: เขียนโค้ดเพื่อกำหนดพฤติกรรมของวัตถุผ่านเมท็ดที่เขียนไว้ในคลาสโดยใช้ค่าพื้นฐาน และกำหนดผลลัพธ์ของการเรียกใช้เมท็ดเหล่านี้

  • 3.5.A.1 เมท็ด void ไม่คืนค่า หัวเมท็ดจะcontains คีย์เวิร์ด void ก่อนชื่อเมท็ด
  • 3.5.A.2 เมท็ดที่ไม่ใช่ void จะคืนค่าเดียว หัวเมท็ดจะ包含 return type แทนที่คีย์เวิร์ด void
  • 3.5.A.3 ในเมท็ดที่ไม่ใช่ void จะประเมินนิพจน์การคืนค่าที่สอดคล้องกับ return type และคืนค่าออก สิ่งนี้เรียกว่า return by value
  • 3.5.A.4 คีย์เวิร์ด return ใช้เพื่อส่งการควบคุมกลับไปยังจุดที่เมท็ดหรือตัวสร้างถูกเรียกใช้ โค้ดใดๆ ที่อยู่ถัดไปจากคำสั่ง return จะไม่เคย被执行 การ执行คำสั่ง return ภายใน selection หรือ iteration statement จะหยุดการทำงานของ statement นั้นและออกจากメท็ดหรือตัวสร้าง
  • 3.5.A.5 เมท็ด accessors อนุญาตให้วัตถุของคลาสอื่นรับสำเนาของค่าตัวแปรอินสแตนซ์หรือตัวแปรคลาส เมท็ด accessor เป็นเมท็ดที่ไม่ใช่ void
  • 3.5.A.6 เมท็ด mutators (modifiers) เป็นเมท็ดที่เปลี่ยนแปลงค่าของตัวแปรอินสแตนซ์หรือตัวแปรคลาส เมท็ด mutator มักจะเป็นเมท็ด void
  • 3.5.A.7 เมท็ดที่มีพารามิเตอร์จะรับค่าผ่านพารามิเตอร์เหล่านั้นและใช้ค่าเหล่านั้นในการดำเนินงานตามหน้าที่ของเมท็ด
  • 3.5.A.8 เมื่ออาร์กิวเมนต์เป็นค่าพื้นฐาน พารามิเตอร์จะถูกเริ่มต้นด้วยสำเนาของค่านั้น การเปลี่ยนแปลงพารามิเตอร์ไม่มีผลต่ออาร์กิวเมนต์ที่เกี่ยวข้อง

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

A method has a signature, a return type, and a body. An accessor (getter) 访问器 returns information without changing the object; a mutator (setter) 修改器 changes a field. A method returning a value must have a return of the right type on every path; a void method returns nothing.

ไทย

เมทόδมี signature, ประเภทข้อมูลกลับคืน และ body accessor (getter) ส่งกลับข้อมูลโดยไม่เปลี่ยนออบเจกต์; mutator (setter) เปลี่ยน field เมทόδที่ส่งกลับค่าต้องมี return ที่มีประเภทที่ถูกต้องในทุกเส้นทาง; เมทόδ void ไม่ส่งอะไรกลับมาเลย

public void setScore(int s) { score = s; }   // mutator
public String toString() { return name + ": " + score; }
Explore · ⁨สำรวจ⁩

Follow a method call and its return · ⁨ติดตามการเรียกเมทόδและการส่งคืนค่า⁩

Calling a method pushes a frame with its parameters; when it hits return, the frame pops and the value goes back to the caller. · ⁨การเรียก method จะเพิ่ม frame ที่มีพารามิเตอร์ลงบนสัก เมื่อเจอ return frame นั้นจะถูกนำออกและค่าถูกส่งกลับไปยังผู้เรียก⁩

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
mutator (setter)/mjuːˈteɪtə/ mutator (setter)
static (class) variable/ˈstætɪk ˈveərɪəbl/ ตัวแปรคลาส (ตัวแปรแบบสถิต)
3.6

Passing and Returning References of an Object · ⁨การส่งและส่งกลับอ้างอิงของออบเจกต์⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.6.A: Develop code to define behaviors of an object through methods written in a class using object references and determine the result of calling these methods.

  • 3.6.A.1 When an argument is an object reference, the parameter is initialized with a copy of that reference; it does not create a new independent copy of the object. If the parameter refers to a mutable object, the method or constructor can use this reference to alter the state of the object. It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
  • 3.6.A.2 When the return expression evaluates to an object reference, the reference is returned, not a reference to a new copy of the object.
  • 3.6.A.3 Methods cannot access the private data and methods of a parameter that holds a reference to an object unless the parameter is the same type as the method's enclosing class.
ไทย

จุดประสงค์การเรียนรู้ 3.6.A: เขียนโค้ดเพื่อกำหนดพฤติกรรมของวัตถุผ่านเมท็ดที่เขียนไว้ในคลาสโดยใช้อ้างอิงวัตถุ และกำหนดผลลัพธ์ของการเรียกใช้เมท็ดเหล่านี้

  • 3.6.A.1 เมื่ออาร์กิวเมนต์เป็นอ้างอิงถึงวัตถุ (object reference) พารามิเตอร์จะถูกเริ่มต้นด้วยสำเนาของอ้างอิงนั้น; ไม่มีการสร้างสำเนาอิสระใหม่ของวัตถุ หากพารามิเตอร์อ้างอิงถึงวัตถุที่แก้ไขได้ (mutable object) เมธอดหรือคอนสตรักเตอร์สามารถใช้ อ้างอิงนี้ในการเปลี่ยนแปลงสถานะของวัตถุ เป็นปฏิบัติที่ดีในการเขียนโปรแกรมที่ไม่ควรแก้ไขวัตถุที่แก้ไขได้ซึ่งส่งมาเป็นพารามิเตอร์ เว้นแต่จะระบุไว้ในข้อกำหนด
  • 3.6.A.2 เมื่อนิพจน์การคืนค่า (return expression) ประเมินผลเป็นอ้างอิงถึงวัตถุ จะมีการคืนค่าอ้างอิงนั้น ไม่ใช่การคืนค่าอ้างอิงไปยังสำเนาใหม่ของวัตถุ
  • 3.6.A.3 เมธอดไม่สามารถเข้าถึงข้อมูลส่วนตัวและเมธอดส่วนตัวของพารามิเตอร์ที่ถืออ้างอิงถึงวัตถุได้ เว้นแต่พารามิเตอร์จะมีประเภทเดียวกันกับคลาสที่ห่อหุ้มเมธอด

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English
= copies the reference, not the object

When you pass an object to a method, Java copies the reference, so the method acts on the same object – changes to its fields are visible to the caller. (Primitives are copied by value, so changes to them are not.) A method can also return a reference to an object. Because a String is immutable, passing one is safe; passing a mutable object lets a method change it.

Exam skill: know that mutating an object's fields inside a method affects the original, but reassigning the parameter (param = new...) does not affect the caller.

Worked example. Suppose s is a Student with score 50, and we call tweak(s):

Line (1) changes the object s points to, so the caller now sees 100. Line (2) makes the method's own copy of the reference point at a fresh object – the caller's s is untouched – and line (3) affects only that new object. After the call, s.getScore() is 100: the mutation stuck, the reassignment did not.

ไทย
= copies the reference, not the object

เมื่อคุณส่งออบเจกต์เข้าเมทόδ Java จะคัดลอก reference ดังนั้นเมทόδจึงกระทำกับ ออบเจกต์เดียวกัน – การเปลี่ยนแปลงใน fields ของมันจะมองเห็นได้โดยผู้เรียก (Primitives ถูกคัดลอกด้วยค่า ดังนั้นการเปลี่ยนแปลงจึงไม่เห็น) เมทόδยังสามารถ ส่งกลับ reference ไปยังออบเจกต์ได้ เนื่องจาก String เป็นสิ่งที่ไม่สามารถเปลี่ยนแปลงได้ การส่งมันจึงปลอดภัย; การส่งออบเจกต์ที่เปลี่ยนแปลงได้会让メソッドเปลี่ยนมันได้

Java ส่งค่าด้วยค่า: เมทόδรับสำเนา; การส่งจริงด้วย reference ซึ่ง Java ไม่มี จะช่วยให้เมทόδกำหนดค่าตัวแปรของผู้เรียกใหม่ได้
Java ส่งค่าด้วย ค่าเสมอ (ซ้าย): เมทόδได้รับสำเนาของ reference การส่งจริงด้วย reference (ขวา) – ซึ่ง Java ไม่มี – จะช่วยให้เมทόδกำหนดค่าตัวแปรของผู้เรียกใหม่ได้.

ทักษะสอบ: ต้องรู้ว่าการทำ mutate ใน fields ของออบเจกต์ภายในเมทόδจะมีผลต่อของเดิม แต่ การกำหนดค่าใหม่ ให้พารามิเตอร์ (param = new...) จะไม่มีผลต่อผู้เรียก

ตัวอย่างฝึกฝน. สมมติว่า s เป็น Student ที่คะแนน 50, และเราเรียก tweak(s):

public static void tweak(Student a) {
    a.setScore(100);          // (1) mutates the shared object
    a = new Student("Z", 0);  // (2) repoints the local copy only
    a.setScore(5);            // (3) changes only the new local object
}

บรรทัด (1) เปลี่ยนแปลงออบเจกต์ที่ s ชี้ไปยัง ทำให้ผู้เรียกเห็น 100 บรรทัด (2) ทำให้สำเนาของ reference ภายในเมทόδชี้ไปยังออบเจกต์ใหม่ – s ของผู้เรียกไม่ถูกแตะต้อง – และบรรทัด (3) ส่งผลเฉพาะต่อออบเจกต์ใหม่นั้น หลังการเรียก s.getScore() คือ 100: การ mutate ติดไปแต่การกำหนดค่าใหม่ไม่ได้

3.7

Class Variables and Class Methods · ⁨ตัวแปรคลาสและเมทόδคลาส⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.7.A: Develop code to define behaviors of a class through class methods.

  • 3.7.A.1 Class methods cannot access or change the values of instance variables or call instance methods without being passed an instance of the class via a parameter.
  • 3.7.A.2 Class methods can access or change the values of class variables and can call other class methods.

Learning Objective 3.7.B: Develop code to declare the class variables that belong to the class.

  • 3.7.B.1 Class variables belong to the class, with all objects of a class sharing a single copy of the class variable. Class variables are designated with the static keyword before the variable type.
  • 3.7.B.2 Class variables that are designated public are accessed outside of the class by using the class name and the dot operator, since they are associated with a class, not objects of a class.
  • 3.7.B.3 When a variable is declared final, its value cannot be modified.
ไทย

วัตถุประสงค์การเรียนรู้ 3.7.A: เขียนโค้ดเพื่อกำหนดพฤติกรรมของคลาสผ่านเมธอดคลาส

  • 3.7.A.1 เมธอดคลาสไม่สามารถเข้าถึงหรือเปลี่ยนแปลงค่าของตัวแปรอินสแตนซ์หรือเรียกเมธอดอินสแตนซ์ได้โดยไม่ได้รับอินสแตนซ์ของคลัสมาทางพารามิเตอร์
  • 3.7.A.2 เมธอดคลาสสามารถเข้าถึงหรือเปลี่ยนแปลงค่าของตัวแปรคลาสและสามารถเรียกเมธอดคลาสอื่นๆ ได้

วัตถุประสงค์การเรียนรู้ 3.7.B: เขียนโค้ดเพื่อประกาศตัวแปรคลาสที่เป็นของคลาส

  • 3.7.B.1 ตัวแปรคลาสเป็นของคลาส โดยวัตถุทั้งหมดของคลัสจะแบ่งปันสำเนาเดียวของตัวแปรคลาส ตัวแปรคลาสจะถูกกำหนดโดยคำสำคัญ static ก่อนชนิดของตัวแปร
  • 3.7.B.2 ตัวแปรคลาสที่กำหนดด้วย public สามารถเข้าถึงภายนอกคลาสโดยใช้ชื่อคลาสและตัวดำเนินการจุด เนื่องจากมีความเกี่ยวข้องกับคลาส ไม่ใช่วัตถุของคลาส
  • 3.7.B.3 เมื่อตัวแปรถูกประกาศด้วย final ค่าของมันไม่สามารถแก้ไขได้

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English
static vs instance fields

A static (class) variable 类变量, marked static, is shared by all objects of the class – one copy total (e.g. a counter of how many objects exist). A static method belongs to the class and cannot use instance fields directly. Access them by class name: Student.getCount().

ไทย
static เปรียบเทียบกับ instance fields

ตัวแปร static (class),中标记 static, ถูกแชร์โดย ทุก ออบเจกต์ของคลาส – มีเพียงสำเนาเดียว (เช่น ตัวนับว่ามีออบเจกต์อยู่กี่ตัว) เมทόδ static เป็นของคลาสและไม่สามารถใช้ instance fields โดยตรง เข้าถึงได้โดยชื่อคลาส: Student.getCount().

3.8

Scope and Access · ⁨ขอบเขตและการเข้าถึง⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.8.A: Explain where variables can be used in the code.

  • 3.8.A.1 Local variables are variables declared in the headers or bodies of blocks of code. Local variables can only be accessed in the block in which they are declared. Since constructors and methods are blocks of code, parameters to constructors or methods are also considered local variables. These variables may only be used within the constructor or method and cannot be declared to be public or private.
  • 3.8.A.2 When there is a local variable or parameter with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable within the body of the constructor or method.
ไทย

วัตถุประสงค์การเรียนรู้ 3.8.A: อธิบายว่าตัวแปรสามารถใช้ใน何处ในโค้ด

  • 3.8.A.1 ตัวแปรท้องถิ่น คือตัวแปรที่ประกาศในหัวหรือตัวบล็อกของโค้ด ตัวแปรท้องถิ่นสามารถเข้าถึงได้เฉพาะในบล็อกที่ประกาศไว้เท่านั้น เนื่องจากคอนสตรักเตอร์และเมธอดเป็นบล็อกของโค้ด พารามิเตอร์ของคอนสตรักเตอร์หรือเมธอดจึงถือเป็นตัวแปรท้องถิ่นด้วย ตัวแปรเหล่านี้สามารถใช้ได้เฉพาะภายในคอนสตรักเตอร์หรือเมธอดเท่านั้น และไม่สามารถประกาศให้เป็น public หรือ private ได้
  • 3.8.A.2 เมื่อมีตัวแปรท้องถิ่นหรือพารามิเตอร์ที่มีชื่อกับตัวแปรอินสแตนซ์เหมือนกัน ชื่อตัวแปรจะอ้างอิงไปยังตัวแปรท้องถิ่นแทนที่จะเป็นตัวแปรอินสแตนซ์ภายในตัวบล็อคของคอนสตรักเตอร์หรือเมธอด

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Scope 作用域 is where a name is visible. A local variable declared in a method exists only inside it; a parameter exists only in its method; an instance variable is visible throughout the object. Access modifiers control visibility across classes: private (this class only) versus public (anywhere). Local variables shadow fields of the same name – a source of bugs.

ไทย

Scope คือตำแหน่งที่ชื่อปรากฏให้เห็น local variable ที่ประกาศในเมท่อมจะมีอยู่เฉพาะภายในนั้น; parameter มีอยู่เฉพาะในเมทოდ它那; instance variable เห็นได้ตลอดทั้งออบเจกต์ Access modifiers ควบคุมการมองเห็นข้ามคลาส: private (คลาสนี้เท่านั้น) เทียบกับ public (ทุกที่) Local variables shadow fields ที่มีชื่อเดียวกัน – ซึ่งเป็นแหล่งกำเนิดของข้อผิดพลาด

ตัวแปร global เห็นได้ทุกที่; local variable เห็นได้เฉพาะภายใน block ของมัน
ตัวแปร global เห็นได้ทุกที่; local variable เห็นได้เฉพาะภายใน block ของมัน
Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
Scope/skəʊp/ ขอบเขต
3.9

The this Keyword · ⁨คำว่า this⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 3.9.A: Develop code for expressions that are self-referencing and determine the result of these expressions.

  • 3.9.A.1 Within an instance method or a constructor, the keyword this acts as a special variable that holds a reference to the current object—the object whose method or constructor is being called.
  • 3.9.A.2 The keyword this can be used to pass the current object as an argument in a method call.
  • 3.9.A.3 Class methods do not have a this reference.
ไทย

วัตถุประสงค์การเรียนรู้ 3.9.A: เขียนโค้ดสำหรับนิพจน์ที่อ้างอิงถึงตนเองและกำหนดผลลัพธ์ของนิพจน์เหล่านี้

  • 3.9.A.1 ภายในเมธอดอินสแตนซ์หรือคอนสตรักเตอร์ คำนำหน้า this ทำหน้าที่เป็นตัวแปรพิเศษที่ถืออ้างอิงถึงวัตถุปัจจุบัน—คือวัตถุwhoseメソッドหรือコンストラクタが呼ばれています。
  • 3.9.A.2 คำนำหน้า this สามารถใช้เพื่อส่งวัตถุปัจจุบันเป็นอาร์กิวเมนต์ในการเรียกเมธอด
  • 3.9.A.3 เมธอดคลาสไม่มีอ้างอิงถึง this

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

this is a reference to the current object. Use it to tell a field apart from a parameter with the same name, or to call another method of the same object:

Exam skill: when a constructor or setter's parameter has the same name as a field, you must write this.field = param – without this, the assignment does nothing useful.

ไทย

this อ้างถึง วัตถุปัจจุบัน ใช้เพื่อแยกแยะฟิลด์ออกจากพารามิเตอร์ที่มีชื่อเดียวกัน หรือเรียกเมทόδอื่นของวัตถุเดียวกัน:

public Student(String name, int score) {
    this.name = name;      // this.name is the field; name is the parameter
    this.score = score;
}

ทักษะสำหรับการสอบ: เมื่อพารามิเตอร์ของคอนสตรัคเตอร์หรือเซตเตอร์มีชื่อเดียวกับฟิลด์ คุณ ต้อง เขียน this.field = param – หากไม่มี this การกำหนดค่าจะไม่ได้ผลอะไรเลย

3.9

Exam tips · ⁨ข้อแนะนำสำหรับการสอบ⁩

English
  • Design with methods and classes: encapsulate data as private fields and expose behaviour through public methods.
  • Know the difference between an object and its class, and that objects are passed by value — the parameter gets a copy of the reference, so a method can change the object's state, but reassigning the parameter does not affect the caller (Java has no pass-by-reference).
  • Traverse arrays and ArrayLists safely — size is length vs .size(), and removing during a loop shifts indices.
  • Trace a recursive method to determine its result: find the base case first, then follow each recursive call to its return value (writing recursive code is outside the exam's scope).
  • Recognise inheritance vocabulary — superclass, subclass, method overriding, and that every class is a subclass of Object (designing and implementing inheritance is outside the exam's scope).
ไทย
  • ออกแบบด้วยเมทόδและคลาส: ห่อหุ้มข้อมูลเป็นฟิลด์ private และเปิดใช้งานพฤติกรรมผ่านเมทόδสาธารณะ
  • รู้ความแตกต่างระหว่าง วัตถุ กับ คลาส ของมัน และว่าวัตถุถูกส่งเข้าโดย ค่า — พารามิเตอร์รับสำเนาของรีเฟอเรนซ์ ดังนั้นเมทόδสามารถเปลี่ยนสถานะของวัตถุได้ แต่การกำหนดค่าใหม่ให้พารามิเตอร์จะไม่ส่งผลต่อผู้เรียก (Java ไม่มีระบบส่งเข้าโดยรีเฟอเรนซ์)
  • ท่องผ่านอาร์เรย์และ ArrayList อย่างปลอดภัย — ขนาดคือ length เทียบกับ .size() และการลบองค์ประกอบในระหว่างลูปจะทำให้ดัชนีเลื่อน
  • ติดตาม เมทόδแบบ récursive เพื่อกำหนดผลลัพธ์: หาเคสฐานก่อน แล้วตามการเรียก récursive แต่ละครั้งไปยังค่าที่กลับคืนมา (การเขียนโค้ด récursiveอยู่นอกขอบเขตของการสอบ)
  • จำคำศัพท์เกี่ยวกับ การสืบทอด ได้ — ซูเปอร์คลาส, สับคลาส, การทับเมทόδ และว่าทุกคลาสเป็นสับคลาสของ Object (การออกแบบและดำเนินการสืบทอดอยู่นอกขอบเขตของการสอบ)

Interactive lessons on this topic · ⁨บทเรียนเชิงโต้ตอบสำหรับหัวข้อนี้⁩

Work through it step by step, with instant-check exercises. · ⁨ทำทีละขั้นตอน พร้อมแบบฝึกหัดตรวจสอบผลทันที⁩

Past Papers · ⁨ข้อสอบย้อนหลัง⁩

More topics in AP Computer Science A · ⁨หัวข้อเพิ่มเติมใน AP Computer Science A⁩

Log in or create account · ⁨เข้าสู่ระบบหรือสร้างบัญชี⁩

IGCSE, A-Level & AP