创建一个Person类,包括int no;String name;int age; 属性1、 java 作业 急
答案:4 悬赏:80 手机版
解决时间 2021-11-15 10:00
- 提问者网友:眉目添风霜
- 2021-11-14 23:00
创建一个Person类,包括int no;String name;int age; 属性1、 java 作业 急
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-11-14 23:08
public class Person{
private int no;
private String name;
private int age;
public Person(String info){
String[] arr = info.split("\s*,\s*");
no = Integer.parseInt(arr[0]);
namge = arr[1];
age = Integer.parse(arr[2]);
}
public String toString(){
return String.format("%d, %s, %d", no, name, age);
}
}
private int no;
private String name;
private int age;
public Person(String info){
String[] arr = info.split("\s*,\s*");
no = Integer.parseInt(arr[0]);
namge = arr[1];
age = Integer.parse(arr[2]);
}
public String toString(){
return String.format("%d, %s, %d", no, name, age);
}
}
全部回答
- 1楼网友:蓝房子
- 2021-11-15 02:56
public class Test {
public static void main(String agrs[]) {
Person p = new Person("12345, 张三, 19");
//调用Person对象的构造方法
System.out.println(p.toString());
//创建一个Person对象p1
Person p1 = new Person();
//为p1对象的属性赋值
p1.setAge(22);
p1.setNo(15);
p1.setName("张三");
//将p1对象作为参数构造新的p2函数
//调用对象实例化
Person p2 = new Person(p1);
//调用Person对象的构造方法
System.out.println(p2.toString());
}
calss Person {
private int no;
private String name;
private int age;
public Person() {
}
public Person(Person info) {
this.no = info.getNo();
this.name = info.getName();
this.age = info.getAge();
}
public Person(String info) {
String[] s = info.split(",");
this.no = Integer.parseInt(s[0]);
this.name = s[1];
this.age = Integer.parseInt(s[2]);
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getNo() {
return this.no;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String toString() {
return "no:"+this.no+",name:"+this.name+",age:"+this.age;
}
}
}
public static void main(String agrs[]) {
Person p = new Person("12345, 张三, 19");
//调用Person对象的构造方法
System.out.println(p.toString());
//创建一个Person对象p1
Person p1 = new Person();
//为p1对象的属性赋值
p1.setAge(22);
p1.setNo(15);
p1.setName("张三");
//将p1对象作为参数构造新的p2函数
//调用对象实例化
Person p2 = new Person(p1);
//调用Person对象的构造方法
System.out.println(p2.toString());
}
calss Person {
private int no;
private String name;
private int age;
public Person() {
}
public Person(Person info) {
this.no = info.getNo();
this.name = info.getName();
this.age = info.getAge();
}
public Person(String info) {
String[] s = info.split(",");
this.no = Integer.parseInt(s[0]);
this.name = s[1];
this.age = Integer.parseInt(s[2]);
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getNo() {
return this.no;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String toString() {
return "no:"+this.no+",name:"+this.name+",age:"+this.age;
}
}
}
- 2楼网友:你可爱的野爹
- 2021-11-15 02:19
class Person{
private int No;
private String Name;
private int Age;
public Person(String info)
{
String [] temp = info.split(",");
this.No = Integer.parseInt(temp[0]);
this.Name = temp[1];
this.Age = Integer.parseInt(temp[2]);
}
public String toString()
{
return No+","+Name+","+Age;
}
}
private int No;
private String Name;
private int Age;
public Person(String info)
{
String [] temp = info.split(",");
this.No = Integer.parseInt(temp[0]);
this.Name = temp[1];
this.Age = Integer.parseInt(temp[2]);
}
public String toString()
{
return No+","+Name+","+Age;
}
}
- 3楼网友:平生事
- 2021-11-15 00:45
public class Person{
public int no;
public String name;
public int age;
public Person(String info){
String[] infoStrings=info.trim().split(",");
if (infoStrings.length>=3) {
this.no=Integer.parseInt(infoStrings[0]);
this.name=infoStrings[1];
this.age=Integer.parseInt(infoStrings[2]);;
}
}
public String toString(){
return no+","+name+","+age;
}
public static void main (String[] args){
Person person=new Person("12345,张三,19");
System.out.print(person.toString());
}
}
public int no;
public String name;
public int age;
public Person(String info){
String[] infoStrings=info.trim().split(",");
if (infoStrings.length>=3) {
this.no=Integer.parseInt(infoStrings[0]);
this.name=infoStrings[1];
this.age=Integer.parseInt(infoStrings[2]);;
}
}
public String toString(){
return no+","+name+","+age;
}
public static void main (String[] args){
Person person=new Person("12345,张三,19");
System.out.print(person.toString());
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯