sdsssfa2222 2021-12-16 08:46 采纳率: 100%
浏览 35
已结题

java还没学字符类型的转换,这个应该只有return那部分需要转换的问题吧

img


package zhu;

public class Person {
    public String name;
    public int age;
    public String school;
    public String major;

    public void Person1(String name,int age) {
        this.name =name;
        this.age=age;
    }
    public String Person1() {
        return this.name;
        return this.age;

    }


    public void Person2(String name,int age,String school) {
        this.name =name;
        this.school=school;
        this.age=age;
    }
    public int Person2() {
        return name;
        return school;
        return age;
    }


    public void Person3(String name,int age,String school,String major) {
        this.name =name;
        this.school=school;
        this.age=age;
        this.major=major;
    }
    public String Person3() {
        return name;
        return school;
        return age;
        return major;
    }
}
package zhu;

public class test {
    public static void main(String[] args){
        Person p=new Person();
        p.Person1("小明",10);
        p.Person2("小明",10,"大学");
        p.Person3("小明",10,"大学","机械专业");

        System.out.println(p.Person1());
        System.out.println(p.Person2());
        System.out.println(p.Person3());

    }
}


  • 写回答

4条回答 默认 最新

  • 大鹏cool Java领域优质创作者 2021-12-16 09:09
    关注

    构造方法:Java 类中的构造方法用于初始化对象,从语法上来说,和普通的方法相比,没有返回类型,名称和类名相同,通过 new 关键字调用,执行后返回的是类的实例对象。

    方法重载:方法名相同、参数不同的多个方法称为重载方法。Java 虚拟机会根据参数选取合适的方法调用。参数少的方法调用参数多的重载方法是个减少代码量的小技巧。

    this:this 关键字可用于在构造方法中调用其他构造方法。

    根据上面的规则帮你修改代码如下:

    public class Person {
    
        public String name;
        
        public int age;
        
        public String school;
        
        public String major;
    
        public Person(String n, int a) {
            this(n, a, null);
        }
    
        public Person(String n, int a, String s) {
            this(n, a, s, null);
        }
    
    
        public Person(String n, int a, String s, String m) {
            this.name = n;
            this.age = a;
            this.school = s;
            this.major = m;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", school='" + school + '\'' +
                    ", major='" + major + '\'' +
                    '}';
        }
    }
    
    public class Test {
    
        public static void main(String[] args) throws SQLException, IOException {
            Person p1 = new Person("小明", 10);
            Person p2 = new Person("小明", 10, "大学");
            Person p3 = new Person("小明", 10, "大学", "机械专业");
    
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 12月24日
  • 已采纳回答 12月16日
  • 修改了问题 12月16日
  • 创建了问题 12月16日

悬赏问题

  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多