return age;
}
}
class Student extends Person{
}
public class Ext {
public static void main (String args[]) {
Student stu = new Student();
stu.setAge(10);
System.out.println(stu.getAge());
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Ext.java
root@ubuntu:/home/topeet/guyilian# java Ext
10
**在上面的代码基础上,我们的子类继承了父类,我们可以添加自己的属性还有方法.**
class Person {
private int age;
public void setAge(int age) {
if (age < 0 || age > 200)
age = 0;
else {
this.age = age;
}
}
public int getAge() {
return age;
}
}
class Student extends Person{
private String school;
public void setSchool(String school) {
this.school = school;
}
public String getSchool() {
return school;
}
public Student(String school) {
this.school = school;
}
}
public class Ext2 {
public static void main (String args[]) {
Student stu = new Student(“ustc”);
stu.setAge(10);
System.out.println(stu.getAge());
System.out.println(stu.getSchool());
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Ext2.java
root@ubuntu:/home/topeet/guyilian# java Ext2
10
ustc
**子类继承父类的方法和属性都可以进行覆写,我们在子类覆写父类的printInfo方法。**
class Person {
private int age;
public void setAge(int age) {
if (age < 0 || age > 200)
age = 0;
else {
this.age = age;
}
}
public int getAge() {
return age;
}
public void printInfo() {
System.out.println("age = "+age);
}
}
class Student extends Person{
private String school;
public void setSchool(String school) {
this.school = school;
}
public String getSchool() {
return school;
}
public Student(String school) {
this.school = school;
}
/* override */
public void printInfo() {
System.out.println("school = "+school+"; age = "+getAge());
}
}
public class Ext3 {
public static void main (String args[]) {
Student stu = new Student(“ustc”);
stu.setAge(10);
System.out.println(stu.getAge());
System.out.println(stu.getSchool());
stu.printInfo();
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Ext3.java
root@ubuntu:/home/topeet/guyilian# java Ext3
10
ustc
school = ustc; age = 10
**实例化子类对象时,先调用父类的构造方法,再调用子类的构造方法,super()函数在子类中可以指定调用父类构造函数的类型。**
class Person {
private int age;
public void setAge(int age) {
if (age < 0 || age > 200)
age = 0;
else {
this.age = age;
}
}
public int getAge() {
return age;
}
public void printInfo() {
System.out.println("age = "+age);
}
public Person () {System.out.println("Person ()");}
public Person (int age) {
System.out.println("Person (int age)");
this.age = age;
}
}
class Student extends Person{
private String school;
public void setSchool(String school) {
this.school = school;
}
public String getSchool() {
return school;
}
public Student(String school) {
/* will call the super() */
//super();
super(5);
System.out.println("Student(String school)");
this.school = school;
}
/* override */
public void printInfo() {
System.out.println("school = "+school+"; age = "+getAge());
}
}
public class Ext4 {
public static void main (String args[]) {
Student stu = new Student(“ustc”);
//stu.setAge(10);
System.out.println(stu.getAge());
System.out.println(stu.getSchool());
stu.printInfo();
}
}
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**
**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/d6c403840d07d75fe0e5197a10971456.png)
![img](https://img-blog.csdnimg.cn/img_convert/523a8b6173f20f105c1e245211ef38fb.jpeg)
![img](https://img-blog.csdnimg.cn/img_convert/84fd3317aae2bdc60ce05054f87f5118.png)
![img](https://img-blog.csdnimg.cn/img_convert/96e6ca6a57216cd3e69286ee33dee729.png)
![img](https://img-blog.csdnimg.cn/img_convert/1e5599e1efec765016f2ba6ee0b92ce8.png)
![img](https://img-blog.csdnimg.cn/img_convert/68b8e2cdfcd841d6e7e98b3fff73c638.png)
![](https://img-blog.csdnimg.cn/img_convert/29cd19b92751bf5c5cfcf771e31ec319.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!
0potajJ-1715538490370)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!