推荐学习:《java教程》
构造方法
无参构造
public class A { }
public class A { public A(){ System.out.println("构造方法A()"); }}
public class Test { public static void main(String[] args) { A a = new A(); }}
有参构造
public class A { public A() { } public A(String a, String b) { System.out.println(a + b); }}
public class Test { public static void main(String[] args) { A a = new A("aaa", "bbb"); }}
注意
类的继承
代码如下:
public class Ren { public final String shou = "两只手"; public final String jiao = "两只脚"; public final String yan = "两只眼"; public void chi() { System.out.println("会吃"); } public void he() { System.out.println("会喝"); }}
public class Siji extends Ren { public void kai() { System.out.println("会开车"); }}
public class Test { public static void main(String[] args) { Siji b = new Siji(); System.out.println(b.jiao); System.out.println(b.shou); System.out.println(b.yan); b.chi(); b.he(); b.kai(); }}
方法重写、重载
重写
代码如下:
public class Siji extends Ren { public void kai() { System.out.println("会开车"); } public void chi() { System.out.println("会吃屎"); }}
重载
推荐学习:《java教程》
以上就是JAVA面向对象之继承、构造方法、重写和重载(总结分享)的详细内容,更多请关注php中文网其它相关文章!