本文主要记录一些本人在Java学习中的感觉比较重要的概念(更新中~)
方法
方法类似于C语言中的‘函数’这一概念。
static
public class Test
{
public static void main(String[] args)
{
Person per1 = new Person("John",18,"中国");
Person per2 = new Person("Lucy",26);
Person per3 = new Person("fly-beep",16);
per1.show();
per2.show();
per3.show();
System.out.println("------------------------");
per3.country = "中国台湾";
per1.show();
per2.show();
per3.show();
}
}
class Person
{
public String name;
public int age;
public static String country;
public Person(String name,int age)
{
this.name = name;
this.age = age;
}
public Person(String name,int age,String country)
{
this(name, age);
this.country = country;
}
public void show()
{
System.out.println(this.name + " : " + this.age + " from " + this.country);
}
}
输出:
John : 18 from 中国
Lucy : 26 from 中国
fly-beep : 16 from 中国
------------------------
John : 18 from 中国台湾
Lucy : 26 from 中国台湾
fly-beep : 16 from 中国台湾
static
特点:
A.随着类加载而加载(main方法)
B.优于对象存在
C.被类中所有对象共享
D.可以通过类名调用(推荐使用)
public class Test
{
public static void main(String[] args)
{
System.out.println(Person.num);
}
}
class Person
{
public static int num = 10;
}
输出:
10
static
注意事项:
A.在静态方法中没有this
关键字
B.静态方法只能访问静态成员变量和静态成员方法,非静态方法随意
构造方法
public class Test {
public static void main(String[] args) {
Students stu = new Students("fly-beep",001);
System.out.println("Name : " + stu.getName() + " id : " + stu.getId());
}
}
class Students
{
private String name;
private int id;
public Students()
{
}
public Students(String name,int id)
{
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
里面的
public Students()
{
}
public Students(String name,int id)
{
this.name = name;
this.id = id;
}
都是构造方法,构造方法可以带参数也可以不带参数。Java默认会给你一个不带参数的构造函数,但如果你自己写了构造函数,Java就不会再为你提供无参构造函数。
函数重载(Overload)
函数重载就是在一个类里面定义好多个同名的方法,但是这些方法的参数不一样,(就是参数类型或数目不一样,其他一不一样无所谓)。
public class Test {
public static void main(String[] args) {
Students stu0 = new Students();
System.out.println("Name : " + stu0.getName() + " id : " + stu0.getId());
Students stu1 = new Students("fly-beep");
System.out.println("Name : " + stu1.getName() + " id : " + stu1.getId());
Students stu2 = new Students("fly-beep",001);
System.out.println("Name : " + stu2.getName() + " id : " + stu2.getId());
}
}
class Students
{
private String name;
private int id;
public Students()
{
}
public Students(String name)
{
this.name = name;
}
public Students(String name,int id)
{
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
函数重写(Override)
函数重写是在子类里面把父类的方法重新再写一遍。
方法重写需要注意的点:
- 父类私有方法不能重写
- 子类重写父类方法,访问权限不能降低(最好保持一致)
- 父类静态方法子类也必须通过静态方法重写(必须两边都有
static
,缺一个都不行)
访问权限:
public
> protected
> default
> private
(若不写即为default
)
public class Test {
public static void main(String[] args) {
Cat cat = new Cat();
cat.drink();
cat.eat();
cat.sleep();
}
}
class Animal
{
public void eat()
{
System.out.println("eat");
}
public void sleep()
{
System.out.println("sleep");
}
public static void drink()
{
System.out.println("drink");
}
}
class Cat extends Animal
{
@Override
public void eat()
{
System.out.println("eat rabbit");
}
@Override
public void sleep()
{
System.out.println("sleep well");
}
public static void drink()
{
System.out.println("drink water");
}
}
this&super
this--->当前类对象引用
super--->父类的对象引用
主要可以分为以下三种情况:
- 1.成员变量
public class Test
{
public static void main(String[] args)
{
Child child = new Child();
child.show();
}
}
class Father
{
public int num = 30;
}
class Child extends Father
{
public int num = 20;
public void show()
{
int num = 10;
System.out.println(num);
System.out.println(this.num);
System.out.println(super.num);
}
}
输出如下:
10
20
30
- 2.构造方法
public class Test
{
public static void main(String[] args)
{
Child child = new Child();
}
}
class Father
{
public Father()
{
System.out.println("Father的无参构造函数");
}
}
class Child extends Father
{
public Child()
{
System.out.println("Child的无参构造函数");
}
}
输出:
Father的无参构造函数
Child的无参构造函数
子类所有的构造方法(包括有参,无参)都会默认采用父类的构造方法
子类的构造方法第一句默认都是
super();
子类至少有一个访问父类的构造方法,否则父类数据就没有初始化
this();
super();
上面的两句一定要放在构造方法第一句
- 3.成员方法
从子类里面调用方法:
– 先从子类中找
– 再从父类中找
– 一直找不到就报错
public class Test
{
public static void main(String[] args)
{
Child child = new Child();
child.show();
Father father = new Father();
father.show();
}
}
class Father
{
public void show()
{
System.out.println("Father show");
}
}
class Child extends Father
{
public void show()
{
System.out.println("Child show");
}
}
输出:
Child show
Father show
final
final
修饰类
该类不能被继承
final
修饰方法
该方法不能被重写
final
修饰变量
该变量不能被赋值,该变量其实是常量(自定义常量)
public class Test
{
public static void main(String[] args)
{
int x = 10;
x = 100;
System.out.println(x);
System.out.println("--------------------");
final int y = 10;
//y = 100 ERROR
System.out.println(y);
System.out.println("--------------------");
Student stu = new Student();
System.out.println(stu.age);
stu.age = 100;
System.out.println(stu.age);
System.out.println("--------------------");
final Student stu1 = new Student();
System.out.println(stu1.age);
stu1.age = 100;
System.out.println(stu1.age);
//stu1 = new Student(); ERROR 这改变了地址
}
}
class Student
{
int age = 10;
}
输出:
100
--------------------
10
--------------------
10
100
--------------------
10
100
注意
final
修饰引用类型,引用类型的地址不能变,但是值是可以变的 !
多态
某一个事物在不同时间表现出不同的状态。
多态前提:
A.要有继承关系
B.要有方法重写
C.要有父类
多态中成员访问特点
重要
A.成员变量
编译看左边,运行看左边
B.构造方法
创建子类对象时,访问父类的构造方法,要对父类数据进行初始化
C.成员方法
编译看左边,运行看右边
D.静态方法
编译看左边,运行看左边
public class Test
{
public static void main(String[] args)
{
Father father =new Child();
System.out.println(father.num); //运行看左边
father.show(); //运行看右边
father.show1(); //运行看左边
}
}
class Father
{
public int num = 10;
public void show()
{
System.out.println("Father is handsome");
}
public static void show1()
{
System.out.println("static Father");
}
}
class Child extends Father
{
public int num = 100;
public void show()
{
System.out.println("Child is very handsome");
}
public static void show1()
{
System.out.println("static Child");
}
}
输出:
10
Child is very handsome
static Father
但是多态有一个问题,不能使用子类独有的方法,于是我们就有了向下转型的概念:
public class Test
{
public static void main(String[] args)
{
Father father =new Child();
//father.show1(); ERROR
Child child = (Child)father;
child.show1();
}
}
class Father
{
}
class Child extends Father
{
public void show1()
{
System.out.println("Father,come to use me");
}
}
输出:
Father,come to use me
有向下转型就有向上转型:
public class Test
{
public static void main(String[] args)
{
Father father = new Child();
//father.show1(); ERROR
Child child = (Child)father;
child.show1();
}
}
class Father
{
}
class Child extends Father
{
public void show1()
{
System.out.println("Father,come to use me");
}
}
输出:
Father,come to use me
抽象类
抽象类的子类:
A.如果不想抽象抽象方法,则子类是一个抽象类
B.重写所有抽象方法,子类是一个具体的类
对于抽象类还是分以下三种情况进行讨论:
- 成员变量
常量/变量
-
构造函数
可以有构造函数,用于子类初始化变量 -
成员方法
– 抽象方法:强制子类做
– 非抽象方法:子类继承
abstract
不能与以下关键字共存:
private
冲突,不能重写
final
冲突,不能重写
static
没有意义,static
要有方法体,而abstract
没有
接口
接口就是功能的扩展,并没有方法的实现
接口要使用多态的方式实例化
接口的子类可以是抽象类(但意义不大)
接口的方法一般都是具体类,但是要重写接口中所有的抽象方法
接口中注意项:
A.成员变量
- 只能是常量,并且是静态
- 默认修饰符:
public static final
B.构造方法
- 接口没有构造方法
C.成员方法
- 只能是抽象方法
- 默认修饰符:
public abstract
类和接口关系:
A.类与类
- 继承关系,只能单继承,但可以多层继承
B.接口与类
- 实现关系,可以单实现,也可以多实现
- 可以一个类实现多个接口
C.接口与接口
- 继承关系,可以单继承,也可以多继承
接口和抽象类区别:
A.成员区别:
-
抽象类
– 成员变量:可以变量,可以常量
– 构造方法:有
– 成员方法:可以抽象,可以非抽象 -
接口
– 成员变量:只能是常量
– 成员方法:只能是抽象
B.关系区别
同上“类和接口关系
”:
C.设计理念区别
- 抽象类 被继承体现的是 : “is a”的关系。抽象类中定义的是该继承体系的共性功能
- 接口 被实现体现的是 : “like a”的关系。接口中定义的是该继承体系的扩展功能
泛型
- 类
public class Test<T>
{
}
- 方法
public <T> void Eat(T food)
{
}
- 接口
interface inter<T>
{
}
通配符:
import java.util.ArrayList;
import java.util.Collection;
public class Test
{
public static void main(String[] args)
{
//泛型如果明确写,前后必须一致
Collection<?> collection1 = new ArrayList<Object>();
Collection<?> collection2 = new ArrayList<Animal>();
Collection<?> collection3= new ArrayList<Dog>();
Collection<?> collection4 = new ArrayList<Cat>();
//extends E : 向下限定,E及其子类
//Collection<? extends Animal> collection5 = new ArrayList<Object>(); ERROR
Collection<? extends Animal> collection6 = new ArrayList<Animal>();
Collection<? extends Animal> collection7= new ArrayList<Dog>();
Collection<? extends Animal> collection8 = new ArrayList<Cat>();
//?extends E : 向上限定,E及其父类
Collection<? super Animal> collection9 = new ArrayList<Object>();
Collection<? super Animal> collection10 = new ArrayList<Animal>();
//Collection<? super Animal> collection11 = new ArrayList<Dog>(); ERROR
//Collection<? super Animal> collection12 = new ArrayList<Cat>(); ERROR
}
}
class Animal
{
}
class Dog extends Animal
{
}
class Cat extends Animal
{
}
可变参数
当不知道到底要传入几个参数时,可以使用可变参数的形式来传参:
public class Test
{
public static void main(String[] args)
{
System.out.println("sum : " + sum(10,12,3,6,3));
System.out.println("sum : " + sum(10,12,3));
System.out.println("sum : " + sum(10,12));
System.out.println("sum : " + sum(10,12,3,6));
}
//相当于传进去一个数组
public static int sum(int... s)
{
int sum = 0;
for(int a : s)
{
sum += a;
}
return sum;
}
}
public class Test
{
public static void main(String[] args)
{
String[] array = {"hello","world"};
//asList : 将数组转为列表
List<String> list = Arrays.asList(array);
for (String string : list)
{
System.out.println(string);
}
}
}