Wiky's Blog

Easy coding,easy life.


  • 首页

  • 关于

  • 归档

  • 标签

  • 分类
Wiky's Blog

java基础知识篇之调用顺序

发表于 2017-05-14   |   热度  °C   |   WordCount: 字

看到Java编程思想里面提到的父子类构造器的调用顺序,再加上自己对static的理解,特地写了这个博文。
例子1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class AA{
AA(int id){
System.out.println("A"+id);
}
}
class BB {
BB(int id){
//System.out.println(id);
System.out.println("B"+id);
}
}
class CC extends BB{
private BB a=new BB(1);//
CC(int id){
super(id);//从B100再到B1可以看出调用顺序为先父类构造器,再到自己的域的声明
System.out.println("C"+id);
}
}
public class DD extends CC{
public static int sid=1;
static{sid++;}
public int id=100;
public static BB b1=new BB(sid);
private AA a=new AA(id);//A
private BB b=new BB(id);//B
static{sid++;}
private CC c=new CC(id);//BC
{//代码块和域执行顺序为同等级别,按顺序执行
sid++;
id++;
}
DD(){
super(sid);
System.out.println("D"+id);
}
public static void main(String[] args){
new DD();//
}
}

阅读全文 »
Wiky's Blog

java基础知识4

发表于 2017-05-14   |   热度  °C   |   WordCount: 字

abstract

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
abstract class Person{//不需要abstract方法也可以声明为abstract类
private String name;
public String getName(){
return name;
}
abstract String getDecription();//虚类里面只能有声明,不能有具体的实现
}
class Student extends Person{
@Override
String getDecription() {//子类继承父类,需实现父类的abstract方法
// TODO Auto-generated method stub
return "Student";
}
}
public class abstractTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p;
//p=new Person();编译不通过,不能创建虚类的对象
p=new Student();
System.out.println( p.getDecription());
}
}

阅读全文 »
Wiky's Blog

java基础知识3

发表于 2017-05-14   |   热度  °C   |   WordCount: 字

可变参数
Java SE5.0以后,提供了可变参数的调用方法:语法:int… arr
下面是一些需要特别注意的点:
public class VariableMethod {

public static  void showarray(int a,int... arr){
    System.out.println("a:"+a);
    for(int i:arr){
        System.out.print(i+" ");
    } 
    System.out.println();
}
//编译不通过,不支持两个可变参数
/*public static  void showarray(int a,int... arr,String...b){
    System.out.println("a:"+a);
    for(int i:arr){
        System.out.print(i+" ");
    } 
    System.out.println();
}*/
//不支持重载void showarray(int a,int... arr),下面的方法编译不通过
/*public static  void showarray(int a,int[] arr){
    //System.out.println("a:"+a);
    for(int i:arr){
        System.out.print(i+" ");
    } 
    System.out.println();
}    */
//编译不通过,可变参数需要放在最后一个参数
/*public static void showarr(int... arr,String name){

}*/
public static void showarray(int a,int b,int c){
       System.out.println(a+" "+b+" "+c);
}

 public static void   main(String[] args){
    showarray(1,2,3);//优先匹配不可变参数的方法,输出1 2 3 而不是a:1
    //showarray(1,2,3);
    //可以把数组传递给可变参数的最后一个参数
    showarray(2,new int[]{1,2,3});
 }

}

阅读全文 »
Wiky's Blog

java基础知识1

发表于 2017-05-14   |   热度  °C   |   WordCount: 字

public,protected,private可见性
public:所有类都可见;
protected:包内所有类以及其他包的子类;
private:己类以及内部类可见;
默认(不加修饰符):包内可见;
static:
静态域:被static修饰的域,它属于类,不属于任何对象实例;
static修饰的方法,属于类,可通过类来调用,同时返回类型也必须是static的类型(基本类型);
不能直接访问非静态对象和方法,不能使用super和this;
也就意味着只能操作静态域了。
static块:
在类加载时被创建,优先于非static块执行;

总而言之,static修饰的东西会在类加载时准备好,脱离对象执行;

阅读全文 »
Wiky's Blog

java基础知识2

发表于 2017-05-14   |   热度  °C   |   WordCount: 字

继承/多态
父类:雇员类,有id,name,salary域,以及相应的getter和setter;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Employee{
private int id;
private double salary;
private String name;
public Employee(){
}
public Employee(int id,double salary,String name){
this.id=id;
this.salary=salary;
this.name=name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary() {
return salary;
}
}

阅读全文 »
123
Wiky

Wiky

屏上得来不觉浅,甚要绝知此事要躬行

25 日志
10 标签
GitHub Weibo 知乎
© 本站总访问量次 本站访客数人次 2017 Wiky
由 Hexo 强力驱动
主题 - NexT.Muse