tailieunhanh - Lecture Introduction to programming with Java - Chapter 9: Classes with class members

Chapter 9 - Classes with class members. This chapter presents the following content: Class Variables, class methods, how to access class members, when to use class members, class constants, example program using class members. | Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program Using Class Members 1 Class Variables Based on what you've learned so far, when you're working on an object-oriented program, you should envision separate objects, each with their own set of data and behaviors (instance variables and instance methods, respectively). That's a valid picture, but you should be aware that in addition to data and behaviors that are specific to individual objects, you can also have data and behaviors that relate to an entire class. Since they relate to an entire class, such data and behaviors are referred to as class variables and class methods, respectively. For a particular class, each of the class's objects has its own copy of the class's instance variables. For a particular class, each of the class's objects shares a single copy of the class's class variables. For a mouse growth simulation program, name some appropriate instance variables and some appropriate class variables. 2 Class Variables If you'd like a variable to be shared by all the objects within the variable's class, make it a class variable by using the static modifier in its declaration: static ; Example: public class Mouse { private static int mouseCount; private static double averageLifeSpan; . Class variables are declared at the top of the class, above all the methods. 3 Class Variables Class variables use the same default values as instance variables: integer types get 0 floating point types get boolean types get false reference types get null What are the default values for the class variables in this code fragment? public class Mouse { private static int mouseCount; private static double averageLifeSpan; private static String researcher; private static int simulationDuration = 730; . Initializations are allowed. 4 Scope You can access a class . | Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program Using Class Members 1 Class Variables Based on what you've learned so far, when you're working on an object-oriented program, you should envision separate objects, each with their own set of data and behaviors (instance variables and instance methods, respectively). That's a valid picture, but you should be aware that in addition to data and behaviors that are specific to individual objects, you can also have data and behaviors that relate to an entire class. Since they relate to an entire class, such data and behaviors are referred to as class variables and class methods, respectively. For a particular class, each of the class's objects has its own copy of the class's instance variables. For a particular class, each of the class's objects shares a single copy of the class's class variables. For a mouse growth simulation .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.