Java 类与对象:实例字段、方法和构造函数详解
1. 实例字段和方法
1.1 基本概念
每个类的实例都有自己的一组字段,这些字段被称为实例字段。可以创建同一个类的多个实例,并在每个实例的字段中存储不同的值。操作类实例的方法被称为实例方法。
1.2 示例代码
以下是一个创建 Rectangle 类多个实例的示例代码:
import javax.swing.JOptionPane;
/**
* This program creates three instances of the
* Rectangle class.
*/
public class RoomAreas {
public static void main(String[] args) {
double number; // To hold a number
double totalArea; // The total area
String input; // To hold user input
// Create three Rectangle objects.
Rectangle kitchen = new Rectangle();
Rectangle bedroom = new Rectangle();
Rectangle den = new Rectangle();
// Get and s
超级会员免费看
订阅专栏 解锁全文
1552

被折叠的 条评论
为什么被折叠?



