1.权限修饰符
/*权限修饰符
-
public
-
private
-
protected
*/
权限修饰符练习
public class zhishidian {
public static void main(String[] args) {
hekai f = new hekai();
//f.show();//查看不了
f.show2();
f.show3();
f.show4();
}
}
public class hekai {
private void show() {
System.out.println(“show”);
}
void show2() {
System.out.println("show2");
}
protected void show3() {
System.out.println("show3");
}
public void show4() {
System.out.println("show4");
}
}
- 链式编程
/*
链式编程。
每次调用完毕方法后,返回的是一个对象。
*/
public class StudentTest3 {
public static void main(String[] args) {
StudenDemo sd = new StudenDemo();
Studen5 s = sd.getStuden5();
s.study();
//sd.getStudent().study();
}
}
class Studen5 {
public void study() {
System.out.println(“Good Good Study,Day Day Up”);
}
}
class StudenDemo {
public Studen5 getStuden5() {
return new Studen5();//连式编程返回的是一个对象
}
}
3.形式参数
/*
形式参数:
引用类型
类名:(匿名对象的时候其实我们已经讲过了) 需要的是该类的对象
抽象类:需要的是该抽象的类子类对象
接口:需要的是该接口的实现类对象
*/
public class StudentTest {
/*1.类名
* 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.public static void main(String[] args) {
//目前是没有办法的使用的
//因为抽象类没有对应的具体类
//那么,我们就应该先定义一个具体类
//需求:我要使用PersonDemo类中的method()方法
PersonDemo pd = new PersonDemo();
Person p = new Student();
pd.method(p);
}
*/
public static void main(String[] args) {
//需求:我要测试LoveDemo类中的love()方法
LoveDemo ld = new LoveDemo();
Love l = new Teacher(); //接口 l=new 接口类();
ld.method(l);
}
}
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,爱林青霞”);
}
}
/*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 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();
}
}
*/