java基础之抽象类与接口的形式参数和返回值

时间:2022-02-05 21:49:33

抽象类与接口形式参数和返回值问题

1、形参问题

/*
1、形式参数:
基本类型(太简单,不是我今天要讲解的)
引用类型
(1)类名:(匿名对象的时候其实我们已经讲过了) 需要的是该类的对象
(2)抽象类:
(3)接口
*/
class Student {
public void study() {
System.out.println("Good Good Study,Day Day Up");
}
} class StudentDemo {
public void method(Student s) { //ss; ss = new Student(); Student s = new Student();
s.study();
}
} class StudentTest {
public static void main(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());
}
}
/*
2、形式参数:
基本类型(太简单,不是我今天要讲解的)
引用类型
类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象
抽象类:需要的是该抽象的类子类对象
接口
*/
abstract class Person {
public abstract void study();
} class PersonDemo {
public void method(Person p) {//p; p = new Student(); Person p = new Student(); //多态
p.study();
}
} //定义一个具体的学生类
class Student extends Person {
public void study() {
System.out.println("Good Good Study,Day Day Up");
}
} class PersonTest {
public static void main(String[] args) {
//目前是没有办法的使用的
//因为抽象类没有对应的具体类
//那么,我们就应该先定义一个具体类
//需求:我要使用PersonDemo类中的method()方法
PersonDemo pd = new PersonDemo();
Person p = new Student();
pd.method(p);
}
}
/*
3、形式参数:
基本类型(太简单,不是我今天要讲解的)
引用类型
类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象
抽象类:需要的是该抽象的类子类对象
接口:需要的是该接口的实现类对象
//定义一个爱好的接口
interface Love {
public abstract void love();
} class LoveDemo {
public void method(Love l) { //l; l = new Teacher(); Love l = new Teacher(); 多态
l.love();
}
} //定义具体类实现接口
class Teacher implements Love {
public void love() {
System.out.println("老师爱学生,爱Java,爱林青霞");
}
} class TeacherTest {
public static void main(String[] args) {
//需求:我要测试LoveDemo类中的love()方法
LoveDemo ld = new LoveDemo();
Love l = new Teacher();
ld.method(l);
}
}

2、返回值问题

    /*
1、返回值类型
基本类型:(基本类型太简单,我不准备讲解)
引用类型:
类:返回的是该类的对象
抽象类:
接口:
*/
class Student {
public void study() {
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;
return new Student();
}
} class StudentTest2 {
public static void main(String[] args) {
//需求:我要使用Student类中的study()方法
//但是,这一次我的要求是,不要直接创建Student的对象
//让你使用StudentDemo帮你创建对象
StudentDemo sd = new StudentDemo();
Student s = sd.getStudent(); //new Student(); Student s = new Student();
s.study();
}
}
/*
2、返回值类型
基本类型:(基本类型太简单,我不准备讲解)
引用类型:
(1)类:返回的是该类的对象
(2)抽象类:返回的是该抽象类的子类对象
(3)接口:
*/
abstract class Person {
public abstract void study();
} class PersonDemo {
public Person getPerson() {
//Person p = new Student();
//return p; return new Student();
}
} class Student extends Person {
public void study() {
System.out.println("Good Good Study,Day Day Up");
}
} class PersonTest2 {
public static void main(String[] args) {
//需求:我要测试Person类中的study()方法
PersonDemo pd = new PersonDemo();
Person p = pd.getPerson(); //new Student(); Person p = new Student(); 多态
p.study();
}
}
/*
3返回值类型
基本类型:(基本类型太简单,我不准备讲解)
引用类型:
(1)类:返回的是该类的对象
(2)抽象类:返回的是该抽象类的子类对象
(3)接口:返回的是该接口的实现类的对象
*/
//定义一个爱好的接口
interface Love {
public abstract void love();
} class LoveDemo {
public Love getLove() {
//Love l = new Teacher();
//return l; return new Teacher();
}
} //定义具体类实现接口
class Teacher implements Love {
public void love() {
System.out.println("老师爱学生,爱Java,爱林青霞");
}
} class TeacherTest2 {
public static void main(String[] args) {
//如何测试呢?
LoveDemo ld = new LoveDemo();
Love l = ld.getLove(); //new Teacher(); Love l = new Teacher(); 多态
l.love();
}
}

3、链式编程

/*
链式编程。
每次调用完毕方法后,返回的是一个对象。
*/
class Student {
public void study() {
System.out.println("Good Good Study,Day Day Up");
}
} class StudentDemo {
public Student getStudent() {
return new Student();
}
} class StudentTest3 {
public static void main(String[] args) {
//如何调用的呢?
StudentDemo sd = new StudentDemo();
//Student s = sd.getStudent();
//s.study(); //大家注意了
sd.getStudent().study();
}
}

5、总结

1:形式参数和返回值的问题(理解)
(1)形式参数:
类名:需要该类的对象
抽象类名:需要该类的子类对象
接口名:需要该接口的实现类对象
(2)返回值类型:
类名:返回的是该类的对象
抽象类名:返回的是该类的子类对象
接口名:返回的是该接口的实现类的对象
(3)链式编程
对象.方法1().方法2().......方法n(); 这种用法:其实在方法1()调用完毕后,应该一个对象;
方法2()调用完毕后,应该返回一个对象。
方法n()调用完毕后,可能是对象,也可以不是对象。

版权声明:本文为小平果原创文章,转载请注明:http://blog.csdn.net/i10630226