package com.oop.demo05;
//在java中所有的类都直接或者间接默认继承object
//人 父类
public class Person {
//public 公有的
//protected 受保护的
//default 默认的
//private 私有的
//private int money=10_0000_0000;
protected String name="Tom";
public Person() {
System.out.println("Person无参执行了");
}
//私有的东西无法被继承
public void print(){
System.out.println("Person");
}
/*public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void say(){
System.out.println("说了一句话");
}
*/
}
package com.oop.demo05;
//学生 is 人 子类
//子类继承了父类就会拥有父类的全部方法,私有的方法以及成员变量无法继承
public class Student extends Person{
String name="Marry";
public Student() {
//隐藏代码:默认调用了父类的无参构造
super();//调用父类的构造器必须要在子类构造器的第一行
//this(name:"hello");
System.out.println("Student无参构造执行");
}
public Student(String name) {
this.name = name;
}
public void test1(String name){
print();
this.print();
super.print();
}
public void print(){
System.out.println("Student");
}
public void test(String name){
System.out.println(name);//zhangsan
System.out.println(this.name);//Marry
System.out.println(super.name);//Tom
}
}
package com.oop.demo05;
//继承
public class A extends B{
//重写
@Override//注解,有功能的注释
public void test() {
System.out.println("A=>test()");
}
}
package com.oop.demo05;
//重写都是方法的重写与属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
package com.oop.demo05;
import com.oop.demo05.Student;
public class Application {
//静态方法和非静态方法区别很大
//静态方法,方法的调用只跟左边定义的数据类型有关
public static void main(String[] args) {
Student student=new Student();
//student.say();
//System.out.println(student.getMoney());
//student.test1("zhangsan");
//方法的调用只跟左边定义的数据类型有关
//非静态:重写
A a=new A();//A
a.test();
//父类的引用指向子类
B b=new A();//子类重写了父类的方法
b.test();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)