instance variables
时间: 2023-04-24 08:00:52 浏览: 135
实例变量是指在类中定义的变量,每个类的实例都有自己的一组实例变量。这些变量的值可以在类的方法中被访问和修改。实例变量通常用来存储对象的状态信息。在Java中,实例变量必须在类的定义中声明,并且可以通过构造函数或setter方法来初始化或修改它们的值。
相关问题
This simple class contains four instance variables, noting that they are defined as ‘public’. Add more codes as below to instantiate the class Person and initialize some instance variables. class Program { static void Main(string[ ] args) { Person David = new Person(); Person Alice = new Person(); // Initialize David David.age = 21; David.name = "David"; David.weight = 185.4; David.height = 72.3; // Initialize Alice Alice.age = 18; Alice.name = "Alice"; Alice.weight = 125.7; Alice.height = 67.1; // print some values Console.WriteLine("David’s age = {0}; David’s weight = {1}",David.age, David.weight); Console.WriteLine("Alice’s age = {0}; Alice’s weight = {1}", Alice.age, Alice.weight); } } Properties in C#: In the previous, we accessed the characteristics of the Person Class using some public methods such as ‘setXXX(…)’ and ‘getXXX()’. C# gives us a more controlled way to access these data, called properties. Search the internet (‘Baidu’ or ‘Google’) about the property and amend the code above accordingly using the property. (Task 1.3)
class Person
{
public int Age { get; set; }
public string Name { get; set; }
public double Weight { get; set; }
public double Height { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person David = new Person();
Person Alice = new Person();
// Initialize David
David.Age = 21;
David.Name = "David";
David.Weight = 185.4;
David.Height = 72.3;
// Initialize Alice
Alice.Age = 18;
Alice.Name = "Alice";
Alice.Weight = 125.7;
Alice.Height = 67.1;
// Print some values
Console.WriteLine("David’s age = {0}; David’s weight = {1}", David.Age, David.Weight);
Console.WriteLine("Alice’s age = {0}; Alice’s weight = {1}", Alice.Age, Alice.Weight);
}
}
In the modified code, we have replaced the public instance variables with properties in the Person class. Properties provide a more controlled way to access and modify data.
Properties are defined using the `{ get; set; }` syntax. They allow you to get the value of the property using `PropertyName` and set the value using `PropertyName = value`.
In the Main method, we now use the properties `Age`, `Name`, `Weight`, and `Height` to initialize the attributes for David and Alice. When printing the values, we access the property values directly.
Using properties instead of public instance variables provides encapsulation and allows for better control over data access.
Design and implement circle class and cylinder class. class circle must be the superclass of class cylinder. class circle cannot have any superclass except the ultimate superclass object. when instantiating the two classes , circles and cylinders can be made and stored in an array list as shown below: Arraylist<Circle> shapes=new Arraylist <Circle>(); shapes.add (new Circle());shapes.add(new Circle(5));shapes.add(new Cylinder());shapes.add(new Cylinder(10,5));for (Circle next:shapes){ System.out.println(next);} Implement both classes by including only the following members in each of the classes:instance variables,overloading consructors,getters,setters,overriding toString,overriding equals
Sure, I can help you with that. Here is the implementation of the Circle and Cylinder classes:
```java
public class Circle {
private double radius;
public Circle() {
this.radius = 0.0;
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Circle with radius " + radius;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Circle)) {
return false;
}
Circle other = (Circle) obj;
return Double.compare(radius, other.radius) == 0;
}
}
public class Cylinder extends Circle {
private double height;
public Cylinder() {
super();
this.height = 0.0;
}
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Cylinder with radius " + getRadius() + " and height " + height;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Cylinder)) {
return false;
}
Cylinder other = (Cylinder) obj;
return super.equals(other) && Double.compare(height, other.height) == 0;
}
}
```
In this implementation, the Circle class has only one instance variable - `radius`, and the Cylinder class has an additional instance variable - `height`. Both classes have two constructors each - one with no arguments and one with `radius` argument for Circle and `radius` and `height` arguments for Cylinder. Both classes have getter and setter methods for their instance variables. The `toString()` method is overridden to return a string representation of the object, and the `equals()` method is overridden to compare two objects based on their instance variables.
You can use these classes as shown in the code snippet you provided, to create circles and cylinders and store them in an ArrayList of Circles.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)