import java.util.Scanner;
class Shape1{
private String color;
private String name;
Shape1()
{
System.out.println("shape 构造器调用了哦");//1
System.out.println(getColor()+" circle be constructed.");//3
}
Shape1(String color,String name)
{
this.color = color;//13
this.name = name;//14
}
public String getColor() {
return color;//16
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;//20
}
public void setName(String name) {
this.name = name;
}
public void printType(){
System.out.println(getColor()+" circle be constructed.");//8 17
System.out.println("this is circle");//9 18
}
public void printName(){
System.out.println("my name is "+getName());//12 21
}
}
class Circle1 extends Shape1
{
private String color;
private String name;
Circle1()
{
System.out.println("Circle 构造器调用了哦");
}
Circle1(String color,String name)
{
this.color = color;//4
this.name = name;//5
}
public String getColor() {
return color;//2 7
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;//11
}
public void setName(String name) {
this.name = name;
}
}
public class ComeToWrite2 {
public static void main(String[] args) {
Shape1 shape1 = new Circle1("red","c1");
shape1.printType();//6
shape1.printName();//10
System.out.println("---------------------------------------");
Shape1 sh = new Shape1("blacnk","cc");
sh.printType();//15
sh.printName();//19
/*
输入
red c1
输出
shape 构造器调用了哦——父类shape()中System.out.println("shape constructor");
null circle be constructed.——父类shape()中System.out.println(getColor()+" circle be constructed.");
输出null原因是子类成员赋值还没开始调用
getColor()这个方法调用的是子类方法中的getColor()
red circle be constructed.——父类printType()
this is circle——父类printType()
my name is c1 ——父类printName()
new Circle(strColor,strName)即子类Circle构造器(String color,String name)调用,
子类默认构造器Circle()没有调用
---------------------------------------
blacnk circle be constructed.
this is circle
my name is cc
父类构造器不会再次调用,getColor()这次用的是父类方法的getColor()
* */
}
}
class Shape1{
private String color;
private String name;
Shape1()
{
System.out.println("shape 构造器调用了哦");//1
System.out.println(getColor()+" circle be constructed.");//3
}
Shape1(String color,String name)
{
this.color = color;//13
this.name = name;//14
}
public String getColor() {
return color;//16
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;//20
}
public void setName(String name) {
this.name = name;
}
public void printType(){
System.out.println(getColor()+" circle be constructed.");//8 17
System.out.println("this is circle");//9 18
}
public void printName(){
System.out.println("my name is "+getName());//12 21
}
}
class Circle1 extends Shape1
{
private String color;
private String name;
Circle1()
{
System.out.println("Circle 构造器调用了哦");
}
Circle1(String color,String name)
{
this.color = color;//4
this.name = name;//5
}
public String getColor() {
return color;//2 7
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;//11
}
public void setName(String name) {
this.name = name;
}
}
public class ComeToWrite2 {
public static void main(String[] args) {
Shape1 shape1 = new Circle1("red","c1");
shape1.printType();//6
shape1.printName();//10
System.out.println("---------------------------------------");
Shape1 sh = new Shape1("blacnk","cc");
sh.printType();//15
sh.printName();//19
/*
输入
red c1
输出
shape 构造器调用了哦——父类shape()中System.out.println("shape constructor");
null circle be constructed.——父类shape()中System.out.println(getColor()+" circle be constructed.");
输出null原因是子类成员赋值还没开始调用
getColor()这个方法调用的是子类方法中的getColor()
red circle be constructed.——父类printType()
this is circle——父类printType()
my name is c1 ——父类printName()
new Circle(strColor,strName)即子类Circle构造器(String color,String name)调用,
子类默认构造器Circle()没有调用
---------------------------------------
blacnk circle be constructed.
this is circle
my name is cc
父类构造器不会再次调用,getColor()这次用的是父类方法的getColor()
* */
}
}