package ch06; class Father { protected int m=2; private int n=4; public Father() {//无参数的构造方法 } public Father(String s) {//有参数的构造方法 } public void methodA() {//公有的方法a System.out.println("调用的是公有方法a"); } void methodB() {//默认的方法b } private void methodC() {//私有函数c } public void testFather(Father f) {//公有的测试函数 } } class Son extends Father{ public void testSon(Son s) { } public static void main(String[] args) { // TODO Auto-generated method stub Son S1=new Son(); System.out.println(S1.m); S1.toString(); S1.methodA(); S1.methodB(); S1.testFather(S1); Father f =new Father(); } } package test1;//构造方法的调用顺序先调用父类的构造方法, class Food1 {//食物类 } class Fruit extends Food1{//水果继承了食物类里面的方法和属性这里为空 Fruit(){ System.out.println("Fruit()");//没有参数的构造方法 } Fruit(String color){//有一个参数的构造方法 System.out.println("Fruit(String)"); } } class Apple extends Fruit{//子类继承水果类 Apple(){ System.out.println("Apple()"); } Apple(String color){ this(color,0); System.out.println("Apple(String)"); } Apple(String color,int count){ super(color); System.out.println("Apple(String,int)"); } } public class Food{ public static void main(String[] args) { Apple a1=new Apple(); System.out.println(); Apple a2=new Apple("red"); System.out.println(); Apple a3=new Apple ("green,10"); } }
演示结果
Fruit() Apple()
Fruit(String) Apple(String,int) Apple(String)
Fruit(String) Apple(String,int) Apple(String)
