封装
public class Package {
private String name;
private int age;
private Package(String name,int age) {
this.name=name;
this.age=age;
}
public void Display() {
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
}
public static void main(String[] args) {
Package page=new Package("王五",40);
page.Display();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a0b0ae447b7de49257cf42989f0696f.png)
继承
class Father{
private String name;
private int age;
protected Father(String name,int age) {
System.out.println("Father构造函数");
this.name=name;
this.age=age;
}
public void Dis() {
System.out.println("他叫"+name);
System.out.println("他已经"+age+"岁了");
}
}
public class Son extends Father{
String sex="男";
public Son(String n,int a) {
super(n,a);
System.out.println("Son的构造函数");
}
public void Dis() {
System.out.println(sex);
}
public void Son0() {
System.out.println("特有方法");
}
public static void main(String[] args) {
Father s=new Son("张三",67);
s.Dis();
Son s1=(Son)s;
s1.Son0();
}
}
class p extends Son{
public p(String n, int a) {
super(n, a);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/544d5b634966f19aa8dee57b66dde0c9.png)
多态
class F{
public void dis() {
System.out.println("父类");
}
public void FF() {
System.out.println("父类无重写");
}
}
class G extends F{
public G(){
System.out.println("子类1");
}
}
public class SonTest extends F{
public SonTest() {
System.out.println("子类2");
}
public void dis() {
System.out.println("子类重写");
}
public void sdis() {
System.out.println("子类独有");
}
public static void main(String[] args) {
F f=new SonTest();
f.FF();
if(f instanceof SonTest) {
SonTest f1=(SonTest)f;
f1.sdis();
}
if(f instanceof G) {
G f2=(G)f;
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f16c087e5bcd839135b9926836ce83a3.png)