自定义类Human,Human类定义3个属性(变量):姓名name,String类型;性别sex,char类型;年龄age,int类型。类中定义构造方法如下:有3个参数的构造方法;无参数的构造方法。类中自定义返回结果的方法showResult(),按下图的每行的格式返回描述Human类信息的字符串。最后,类中增加main方法,分别使用2个构造方法创建对象,并调用其showResult ()方法将对象的信息显示命令行的结果,结果如下: (2)自定义类Person,Person类定义3个protected的属性(变量):姓名name,String类型;性别sex,char类型;年龄age,int类型。类中定义构造方法如下:有3个参数的构造方法,初始化姓名、性别和年龄;有2个参数的构造方法,调用有3个参数的构造方法,初始化姓名、性别、年龄默认初始化为下图的第2行所示的值;无参数的构造方法,调用有3个参数的构造方法,姓名、性别、年龄默认初始化为下图的第1行所示的值。类中自定义返回结果的方法showResult(),按下图的每行的格式返回描述Person类信息的字符串。最后,类中增加main方法,分别使用3个重载的构造方法创建对象,并调用其showResult ()方法将对象的信息显示命令行的结果,结果如下: 提示:无参数的构造方法
时间: 2024-02-01 11:13:30 浏览: 28
1. Human类的代码实现如下:
```java
public class Human {
private String name;
private char sex;
private int age;
// 三个参数的构造方法
public Human(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
// 无参数的构造方法
public Human() {
this.name = "";
this.sex = ' ';
this.age = 0;
}
// 返回结果的方法
public String showResult() {
return "姓名:" + this.name + "\t性别:" + this.sex + "\t年龄:" + this.age;
}
public static void main(String[] args) {
// 创建对象并调用showResult方法
Human human1 = new Human("张三", '男', 20);
System.out.println(human1.showResult());
Human human2 = new Human();
human2.name = "李四";
human2.sex = '女';
human2.age = 18;
System.out.println(human2.showResult());
}
}
```
输出结果为:
```
姓名:张三 性别:男 年龄:20
姓名:李四 性别:女 年龄:18
```
2. Person类的代码实现如下:
```java
public class Person {
protected String name;
protected char sex;
protected int age;
// 有三个参数的构造方法
public Person(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
// 有两个参数的构造方法
public Person(String name, char sex) {
this(name, sex, 18);
}
// 无参数的构造方法
public Person() {
this("", ' ', 20);
}
// 返回结果的方法
public String showResult() {
return "姓名:" + this.name + "\t性别:" + this.sex + "\t年龄:" + this.age;
}
public static void main(String[] args) {
// 创建对象并调用showResult方法
Person person1 = new Person("张三", '男', 20);
System.out.println(person1.showResult());
Person person2 = new Person("李四", '女');
System.out.println(person2.showResult());
Person person3 = new Person();
System.out.println(person3.showResult());
}
}
```
输出结果为:
```
姓名:张三 性别:男 年龄:20
姓名:李四 性别:女 年龄:18
姓名: 性别: 年龄:20
```
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)