/*类名作为形式参数*/
class Student {
publicvoidstudy() {
System.out.println("Good Good Study,Day Day Up");
}
}
class StudentDemo {
publicvoidmethod(Student s) { //ss; ss = new Student(); Student s = new Student();
s.study();
}
}
class StudentTest {
publicstaticvoidmain(String[] args) {
//需求:我要测试Student类的study()方法
Student s = new Student();
s.study();
System.out.println("----------------");
//需求2:我要测试StudentDemo类中的method()方法
StudentDemo sd = new StudentDemo();
Student ss = new Student();
sd.method(ss);
System.out.println("----------------");
//匿名对象用法new StudentDemo().method(new Student());
}
}
/*抽象类作为形式参数*/abstractclassPerson {publicabstractvoid study();
}
classPersonDemo {publicvoid method(Person p) {//p; p = new Student(); Person p = new Student(); //多态
p.study();
}
}
//定义一个具体的学生类classStudentextendsPerson {publicvoid study() {
System.out.println("Good Good Study,Day Day Up");
}
}
classPersonTest {publicstaticvoid main(String[] args) {
//目前是没有办法的使用的//因为抽象类没有对应的具体类//那么,我们就应该先定义一个具体类//需求:我要使用PersonDemo类中的method()方法
PersonDemo pd = new PersonDemo();
Person p = new Student();
pd.method(p);
}
}
/*接口作为形式参数*/interfaceLove {publicabstractvoid love();
}
classLoveDemo {publicvoid method(Love l) { //l; l = new Teacher(); Love l = new Teacher(); 多态
l.love();
}
}
//定义具体类实现接口classTeacherimplementsLove {publicvoid love() {
System.out.println("老师爱学生,爱Java,爱林青霞");
}
}
classTeacherTest {publicstaticvoid main(String[] args) {
//需求:我要测试LoveDemo类中的love()方法
LoveDemo ld = new LoveDemo();
Love l = new Teacher();
ld.method(l);
}
}
/*类作为返回值*/abstractclassPerson {publicabstractvoid study();
}
classPersonDemo {public Person getPerson() {
//Person p = new Student();//return p;returnnew Student();
}
}
classStudentextendsPerson {publicvoid study() {
System.out.println("Good Good Study,Day Day Up");
}
}
classPersonTest2 {publicstaticvoid main(String[] args) {
//需求:我要测试Person类中的study()方法
PersonDemo pd = new PersonDemo();
Person p = pd.getPerson(); //new Student(); Person p = new Student(); 多态
p.study();
}
}
/*抽象类作为返回值*/
class Student {
publicvoidstudy() {
System.out.println("Good Good Study,Day Day Up");
}
}
class StudentDemo {
public Student getStudent() {
//Student s = new Student();//Student ss = s;//Student s = new Student();//return s;returnnew Student();
}
}
class StudentTest2 {
publicstaticvoidmain(String[] args) {
//需求:我要使用Student类中的study()方法//但是,这一次我的要求是,不要直接创建Student的对象//让你使用StudentDemo帮你创建对象
StudentDemo sd = new StudentDemo();
Student s = sd.getStudent(); //new Student(); Student s = new Student();
s.study();
}
}
/*接口作为返回值*/interfaceLove {publicabstractvoid love();
}
classLoveDemo {public Love getLove() {
//Love l = new Teacher();//return l;returnnew Teacher();
}
}
//定义具体类实现接口classTeacherimplementsLove {publicvoid love() {
System.out.println("老师爱学生,爱Java,爱林青霞");
}
}
classTeacherTest2 {publicstaticvoid main(String[] args) {
//如何测试呢?
LoveDemo ld = new LoveDemo();
Love l = ld.getLove(); //new Teacher(); Love l = new Teacher(); 多态
l.love();
}
}
链式编程概述
/*
链式编程。
每次调用完毕方法后,返回的是一个对象。
*/
class Student {
publicvoidstudy() {
System.out.println("Good Good Study,Day Day Up");
}
}
class StudentDemo {
public Student getStudent() {
returnnew Student();
}
}
class StudentTest3 {
publicstaticvoidmain(String[] args) {
//如何调用的呢?
StudentDemo sd = new StudentDemo();
//Student s = sd.getStudent();//s.study();//大家注意了
sd.getStudent().study();
}
}