我发现自己真的没学过java…… o(╯□╰)o 这篇总结可以帮助新手复习,或者查缺补漏(虽然还不太全面),也欢迎老手给指正错误。
访问权限 | 本类 | 本包 | 子类 | 其他包 |
|---|---|---|---|---|
public | ✔ | ✔ | ✔ | ✔ |
protect | ✔ | ✔ | ✔ | |
default | ✔ | ✔ | ||
private | ✔ |
如果不写为默认,即default。
第四点:局部变量定义之后必须赋值才能用,而类中的成员变量不赋值便可以用,这时候 执行的是默认初始化。如下程序输出 0 ;
import java.util.*;
class Student{
int age;
}
class Test1{
public static void main(String [] args){
Student s=new Student();
System.out.print(s.age);
}
}第五点:显式初始化,如下程序输出5;
import java.util.*;
class Student{
int age=5;
}
class Test1{
public static void main(String [] args){
Student s=new Student();
System.out.print(s.age);
}
}可以用来修饰成员变量和成员方法
随着类的加载而加载
优先于对象而存在
被类的所有对象共享(最明显的特点),如下程序:
import java.util.*;
class Student{
static int a=333;
int b=333;
}
class Test1{
public static void main(String [] args){
Student s1=new Student();
Student s2=new Student();
System.out.println(s2.a+" "+s2.b);
s1.a=666;
s1.b=666;
System.out.println(s2.a+" "+s2.b);
}
}
//输出结果:
//333 333
//666 333可以通过类名调用
import java.util.*;
class Student{
static int a=333;
int b=333;
}
class Test1{
public static void main(String [] args){
System.out.println(Student.a);
//System.out.println(Student.b);
//类名直接调用a可以,调用b不可以。
}
}顺序交换不会报错,但是这些约定俗成的东西还是有必要的。
import java.util.*;
class Test1{
public static final int a=1;
static final public int b=1;
final public static int c=1;
static public final int d=1;
final static public int e=1;
public static void main(String [] args){
System.out.print(a);
System.out.print(b);
System.out.print(c);
System.out.print(d);
System.out.print(e);
}
}
// 输出结果:11111保留字是为java预留的关键字,他们虽然现在没有作为关键字,但在以后的升级版本中有可能作为关键字。
待定。。。
多个类中存在相同属性和行为时,将这些内容抽取到单独的一个类中,其他类只要继承到那个类即可。
class student extends person{}子类中所有构造方法默认都会访问父类空参数构造方法: 因为子类会继承父类中的数据,所以子类初始化之前,要先完成父类数据初始化。
同一事物,在不同时刻表现出来不同状态
猫 m = new 猫();
动物 d = new 猫();
//猫可以是一只猫,也可以是动物从子到父,父类引用指向子类对象。我的理解就是本来new了一个Student对象,然后把他(向上)转为父类的Person类型。 用法:
Person p=new Student();举个栗子:
import java.util.*;
class Person{
int per;
void show1(){
System.out.println("this is class of person.");
}
}
class Student extends Person{
int stu;
void show1(){
System.out.println("this is class of Student1.");
}
void show2(){
System.out.println("this is class of Student2.");
}
}
class Test1{
public static void main(String [] args){
Person p=new Student();
p.show1();
//p.show2();//p无法访问子类Student的方法
System.out.println(p.per);
//System.out.println(p.stu);//p无法访问子类Student的成员变量
}
}
// 输出结果:
// this is class of Student1.
// 0从父到子,父类引用转为子类对象。即把Person类型的p(向下)转为Student类型。 用法:
Student s=(Student)p;举个栗子:
import java.util.*;
class Person{
void show1(){
System.out.println("this is class of person.");
}
}
class Student extends Person{
void show2(){
System.out.println("this is class of Student2.");
}
}
class Test1{
public static void main(String [] args){
Person p=new Student();//方法一正确
//Person p=new Person();//方法二会有强制类型转换异常
//Exception in thread "main" java.lang.ClassCastException:
//Person cannot be cast to Student at Test1.main(Test1.java:24)
Student s=(Student)p;
s.show1();
s.show2();
}
}
// 输出结果:
// this is class of person.
// this is class of Student2.class Human {
public void sleep() {
System.out.println("Human sleep..");
}
}
class Male extends Human {
public void sleep() {
System.out.println("Male sleep..");
}
}
class Female extends Human {
public void sleep() {
System.out.println("Female sleep..");
}
}
class MaleStudent extends Male {
public void sleep() {
System.out.println("MaleStudent sleep..");
}
}
class Test1{
public static void main(String[] args) {
dosleep(new Male());
dosleep(new Female());
dosleep(new MaleStudent());
}
public static void dosleep(Human h) {
h.sleep();
}
}
// 输出结果:
// Male sleep..
// Female sleep..
// MaleStudent sleep.当new Male()传入dosleep()时,执行的是Human h=new Male();即向上转型。无论传入的是Male,Female,还是MaleStudent,都只用一个dosleep接收就可以,也不用重载dosleep函数,提高了代码的复用性。
这个栗子有点恶心,但是对理解多态会有帮助的。
class A {
public String show(D obj) {
return ("A and D");
}
public String show(A obj) {
return ("A and A");
}
}
class B extends A{
public String show(B obj){
return ("B and B");
}
public String show(A obj){
return ("B and A");
}
}
class C extends B{}
class D extends B{}
class Test1{
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println("1--" + a1.show(b));
System.out.println("2--" + a1.show(c));
System.out.println("3--" + a1.show(d));
System.out.println("4--" + a2.show(b));
System.out.println("5--" + a2.show(c));
System.out.println("6--" + a2.show(d));
System.out.println("7--" + b.show(b));
System.out.println("8--" + b.show(c));
System.out.println("9--" + b.show(d));
}
}
/* 输出结果:
1--A and A
2--A and A
3--A and D
4--B and A
5--B and A
6--A and D
7--B and B
8--B and B
9--A and D
*/对于前三个,a1有接收A类型和D类型的两个方法,所以第三个a1.show(d)就会调用show(D obj)来接收,而对于1,2的b和c,c继承b,b继承a,所以b,c都向上转型,调用的是show(A obj)。 对于4,5,6,A a2 = new B(); B向上转型成了A类型,就损失了show(B obj),而B中将show(A obj)重写了,所以a2中有以下两个方法:
public String show(D obj) {
return ("A and D");
}
public String show(A obj){
return ("B and A");
}当a2.show()传入的是b或c时,b,c都向上转型成A类型的,调用的是a2中的show(A obj)方法,当传入d时,调用a2中的show(D obj)方法。 对于7,8,9,B b = new B(); 由于B继承A,所以B中有以下三个方法:
public String show(D obj) {
return ("A and D");
}
public String show(A obj){
return ("B and A");
}
public String show(B obj){
return ("B and B");
}当b.show()传入b,c时,b和c都向上转型,然后调用的是show(B obj),传入d时,直接调用show(D obj)。
在java中一个没有方法体的方法应该定义为抽象方法,在类中如果有抽象方法,该类必须定义为抽象类。
没有方法体:public abstract void noMethodBody();
空方法体: public void empertyBody(){}格式
abstract class className{}
public abstract void method();抽象类不能实例化,可以由具体的子类实例化
如有错误,还请指正。
欢迎与我分享你的看法。 转载请注明出处:http://taowusheng.cn/ 微博:寒枫–0-0– 知乎:https://www.zhihu.com/people/tao-wu-sheng 豆瓣:YIFEI