use java constructor overloading. Create a class with any 3 member-variable and 2 member-method.
时间: 2024-02-11 20:06:34 浏览: 37
Sure! Here's an example of a Java class with constructor overloading:
```
public class Person {
private String name;
private int age;
private boolean isMale;
public Person(String name, int age) {
this.name = name;
this.age = age;
this.isMale = true;
}
public Person(String name, int age, boolean isMale) {
this.name = name;
this.age = age;
this.isMale = isMale;
}
public void sayHello() {
System.out.println("Hello, my name is " + name + "!");
}
public void introduceSelf() {
System.out.println("I am " + name + ", " + age + " years old, and " + (isMale ? "male" : "female") + ".");
}
}
```
In this class, we have three member variables (`name`, `age`, and `isMale`) and two member methods (`sayHello()` and `introduceSelf()`).
We also have two different constructors: one that takes a `name` and `age` parameter, and another that takes `name`, `age`, and `isMale` parameters. This allows us to create `Person` objects in different ways depending on our needs.
For example, we could create a `Person` object with just a name and age like this:
```
Person john = new Person("John", 25);
```
Or we could create a `Person` object with a name, age, and gender like this:
```
Person jane = new Person("Jane", 30, false);
```
Hope this helps! Let me know if you have any more questions.