static关键字的使用
对成员变量用static(共享数据)
public class A{
/*
* static 关键字:修饰符,可以修饰成员变量,成员方法
*
* 特点:
* 1.被类的所有对象所共享
* 2.多了一种调用方式,可以通过类名进行调用(推荐使用类名调用)
* 3.随着类的加载而加载,优先于对象存在
*/
public static void main(String[] args) {
Student.school = "jmdx";
Student stu = new Student();
System.out.println(stu.school);
}
}
static修饰成员变量内存图解
对成员方法用static(建立工具类)
重新认识main函数
对static的总结
继承
成员变量的访问(this和super)
方法重写
注意事项:
1.父类中私有方法不能被重写
2.子类重写父类方法时,访问权限必须大于等于父类
Java继承特点:
Java只支持单继承,不支持多继承,但支持多层继承
多继承:会出现子类调用函数不知道调用哪一个
多层继承:一层一层下来,如果有一样名字的函数,那也会进行函数重写,到子类后就只有一种啦
继承中的构造方法
// person.java
package 继承;
public class person {
private String name;
private int age;
public person() {
}
public person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
// teacher.java
package 继承;
public class teacher extends person{
public teacher(String name, int age){
super(name, age);
}
public void teach(){
System.out.println("姓名为"+super.getName()+",年龄为"+super.getAge()
+"岁的老师正在讲课哦");
}
}
// student.java
package 继承;
public class student extends person{
private double score;
public student(String name, int age, double score){
super(name, age);
this.score = score;
}
public void study(){
System.out.println("姓名为"+super.getName()+",年龄为"+super.getAge()
+"岁,成绩为"+score+"分的学生,正在学习");
}
}
// test.java
package 继承;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入老师姓名哦");
String name = sc.next();
System.out.println("请输入老师年龄哦");
int age = sc.nextInt();
teacher t = new teacher(name, age);
t.teach();
System.out.println("请输入学生姓名哦");
String stuname = sc.next();
System.out.println("请输入学生年龄哦");
int stuage = sc.nextInt();
System.out.println("请输入学生成绩哦");
int score = sc.nextInt();
student stu = new student(stuname, stuage, score);
stu.study();
}
}
注意事项:
在子类创建构造方法的时候,默认调用父类的无参构造方法,没有继承的类的话,默认继承object类哦
public class A{
public static void main(String[] args) {
Student stu = new Student();
Student stu1 = new Student(1);
stu.show();
}
}
class person{
public person(){
System.out.println("person无参");
}
public person(int age){
System.out.println("person有参");
}
public void show(){
System.out.println("person---show");
}
}
class Student extends person{
public Student(){
System.out.println("student无参");
}
public Student(int age){
System.out.println("student有参");
}
public void show(){
System.out.println("student---show");
super.show();
}
}
继承内存图
到黑马程序员P109集后半部分具体观看
this和super
特别提醒:这边的super和this本质上是一样的
this是作用在本类里面的,super是对父类调用的时候使用的。当然,在子类继承父类的内容的时候,除了构造方法除外,其他的继承下来了,所以在子类里面如果没有出现重名的情况就可以把super或this略掉
this调用本类的构造方法
class person{
int a, b, c, d;
public person(int a, int b , int c){
this.a = a;
this.b = a;
this.b = a;
}
public person(int a , int b , int c, int d){
// this.a = a;
// this.b = b;
// this.c = c;
this(a, b, c);
this.d = d;
}
}
final关键字
包
常见的导包问题
public class Scanner {
public static void main(String[] args) {
// 这边出现和包名一样的类名 出现导包错误
// 所以这里我们要写具体一点(包名+类名)
Scanner sc = new Scanner(System.in);
java.util.Scanner sc1 = new java.util.Scanner(System.in);
}
}
抽象类
抽象类的注意事项
会出现的冲突问题
接口
定义
public class A{
public static void main(String[] args) {
person p = new person();
p.method();
p.test();
}
}
interface inter{
public abstract void method();
public abstract void test();
}
class person implements inter{
@Override
public void method() {
System.out.println("method---");
}
@Override
public void test() {
System.out.println("test---");
}
}
接口的成员特点
类和接口之间的各种关系
抽象类和接口的对比和分别的应用
抽象类:对事物做抽象(描述事物)
接口:对行为抽象(制定规则)
多态
public class A{
public static void main(String[] args) {
useAnimal(new Dog());
useAnimal(new Cat());
System.out.println("成功运行!");
}
public static void useAnimal(Animal a){
a.eat();
}
}
abstract class Animal{
public abstract void eat();
}
class Dog extends Animal{
public void eat(){
System.out.println("狗吃肉");
}
}
class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
}