java方法的重载

public class Person1 {
    private String name; //姓名
    private int age;      //年龄
    private String school;  //学校
    private String major; //班级
    //构造方法
    public Person1 (String n,int a,String s,String m)
    {
        this.name=n;
        this.age=a;
        this.school=s;
        this.major=m;
    }
    //三个参数
    public Person1 (String n,int a,String s )
    {
        this(n,a,s,null);
    }
    //两个参数
    public Person1 (String n,int a)
    {
        this(n,a,null,null);
    }
    //两个参数
    public Person1 (String a,String m)
    {  
        /*
        this.name=a;
        this.age=Integer.parseInt(null);
        this.school=null;
        this.major=m;
        */
        this(a,Integer.parseInt(null),null,m);
    
    }
    public static void main(String[]args)
    {
        Person1 a=new Person1("马云",18);
        System.out.println("姓名:"+a.name+"\n"+"年龄:"+a.age);
        
    }

}

注:相关知识点,方法构造,方法的重载。

1.方法的构造,在类中方法名必须和类名相同

2.方法的重载,必须满足方法名相同,不同的参数类型或参数个数。跟方法的返回值无关。

3. public Person1 (String n,int a)
    {
        this(n,a,null,null);
    }

括号中的代码也可以这样写

this.name=n;

this.age=a;

this.school=null;

this.major=null;

但要注意整形和字符串之间的转化。例如 this.age=Integer.parseInt(null);因为this.age是整形的右边的赋值必须整形的,而null是字符串类型的。所以要将null转化为整形的

this.age=Integer.parseInt(null);

最新文章

  1. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
  2. Light OJ 1027 - A Dangerous Maze (数学-期望)
  3. [锋利JQ]-图片提示效果
  4. sbin/hadoop-daemon.sh: line 165: /tmp/hadoop-hxsyl-journalnode.pid: Permission denied
  5. (转)函数调用方式与extern "C"
  6. Delphi 的字符及字符串[6] - Char(AnsiChar)、WideChar 与其编码的相互转换
  7. Android5.0新特性——阴影和剪裁(shadow)
  8. onethink常用标签的使用示例
  9. SQL中CONVERT转化函数的用法
  10. Hark的数据结构与算法练习之冒泡排序
  11. asp.net使用httphandler打包多CSS或JS文件以加快页面加载速度
  12. 【转】类中如何引用server.MapPath()
  13. PageView
  14. CSS文档流与块级元素和内联元素(文档)
  15. hdu1026 Ignatius and the Princess I (优先队列 BFS)
  16. Windows端口转发
  17. iOS 给UITextView加一个placeholder
  18. 【Intellij Idea】设置JDK
  19. dubbo接口demo开发
  20. 使用VBA进行JS加密的反混淆,还原JS代码。

热门文章

  1. (转载)delphi中获取汉字的拼音首字母
  2. 也说_T、_TEXT、TEXT、L
  3. Nginx+uWSGI+Django+Python在Linux上的部署
  4. Emmet Documentation
  5. Linux Makefile analysis for plain usr
  6. 配置mybatis错误总结
  7. 哈希,哈希表,哈希Map
  8. 推荐几款jQuery表格插件
  9. hdu 3072
  10. [topcoder]BestRoads