class Pen{
public Pen(){ } //空构造方法
public void show(String msg){
System.out.println(msg);
}
}
class Person{
//1.定义数据成员
String name;
int age;
//2.定义构造方法
//空构造方法,标配
public Person(){ }
//初始化所有数据成员的构造方法
public Person (String aName, int aAge){
name = aName;
age = aAge;
}
//3.定义成员方法
public void writing (Pen pen,String msg){
System.out.println(name + "写");
pen.show(msg);
}
}
public class Demo {
public static void main(String[] args) {
Person shk = new Person("沈括",56);
shk.writing(new Pen(), "梦溪笔谈"); //创建没有对象名的Pen对象
//或者如下代码
//Pen aPen = new Pen(); //命名对象,对象名aPen
//shk.writing(aPen,"梦溪笔谈“);
}
}
标签:String,构造方法,对象,Pen,Person,匿名,使用,msg,public
From: https://blog.csdn.net/2401_83191214/article/details/137125601