Java学习随笔之4:面向对象(上)

Java 也是面向对象的语言,具有 封装 继承 (用 关键字 extends) 多态

类的 修饰符有  public  final abstract (或者完全省略)

 

定义类,成员变量, 方法,构造器  (等同于C#), 不同的是 java 多了一个修饰符 final

 

Public class Person {}

 

Person p;

P = new Person();

P 是一个变量,存储在栈内存里, 它指向一个内存地址 (person 的真正对象,存储在 堆内存中)

 

方法参数: 是 值类型传递, 注意 参数类型是 值类型还是引用类型    (引用类型变量指向的是对象)  

public static void Swap(int a, int b)

{

int temp =0;

temp = a;

a=b;

b=temp;

 

System.out.println("a:" + a +";b:" + b);       

          }

 

In main method:

                      int a = 1;

int b = 2;

 

Swap(a, b);

        

System.out.println("a:" + a +";b:" + b);   //output 1,2

 

可变的形参参数个数  String… books.

可变的参数 必须放到方法的最后面参数中, 它等同于一个数组

public static void TestMethod(int a, String... books)      //等同于定义   TestMethod(int a, String[] books)

{

for(String temp : books)

{

            System.out.println(temp);

}

 

System.out.println(a);

}

 

方法的重载 (不能用返回值类型的不同来作为方法的重载)   //因为系统会糊涂 不知道调用哪个

Int f()

Void f()

当调用方法 直接写  f(), 系统会糊涂,不知道调用哪个

 

Java可以用 类对象来访问 类成员变量(static),但是不推荐用,为了不造成混淆

  class Test

    {

        public static string Name;

    }

 

New Test().Name   //可以访问 不推荐      C#是不可以访问的

 

访问修饰符   private default (=C# internal)   protected public   (从小到大)

 

package Hello;    类似于 C#的命名空间, 可以区分 同名类

Import    类似于C#的 using     import.Lee.Sub.Hello   (lee.Sub 是 packge名字, Hello是 class 名字)

Import.static    导入静态类成员或方法

 

继承:

Java 通过关键字  extends 来做继承    class1 extends class

Java 通过关键字 super 来访问 父类的被重写(隐藏)的成员变量

子类调用父类构造器用关键字  super  (C#用base)

 

多态   (实例变量不具备多态性)

引用变量有两种类型, 编译时类型和运行时类型

如: Person person= new Student();     person 运行时是 Student类型, 但 编译时 是 Person 类型

当调用 方法的时候,会执行 student里的方法, 但当调用成员变量的时候执行的是 Person 的变量 (实例变量不具备多态性) 只有方法具有多态性

 

public class BaseClass

{

public int book = 6;

 

public void Base()

{

        System.out.println("This is one Base method");

}

 

public void Test()

{

System.out.println("This is one base's Test method");

}

 

public class SubClass extends BaseClass

{

public String book = "This is Sub class book veriable";

 

public void Test()

{

System.out.println("This is one Sub's Test method");

}

 

public void Sub()

{

System.out.println("This is one Sub method");

}

 

 

public static void main(String[] args)

{

BaseClass bc = new BaseClass();

bc.Base();   //print the "This is one Base method"

 

SubClass sc = new SubClass();

sc.Test();    //print the "This is one Sub's Test method"

sc.Base();    //print the  "This is one Base method"

 

BaseClass ploymophicBC = new SubClass();

                     ploymophicBC.Test();   //print the "This is one Sub's Test method"

ploymophicBC.Base();    //print the  "This is one Base method"

 

        System.out.println(ploymophicBC.book);    //prinit the 6

}

}

 

 

关键字instanceof,  在进行强制转换之前,先判断前一个对象是否是后一个类的实例,是否可以转换成功 (和 C#中的 is)  只能在有继承关系的对象类之间用

String a = 11 + "";

boolean isInteger = a instanceof Object;      //返回 true

 

int i = 0;

boolean isI = i instanceof String;     //编译出错,int 和 String 没有任何继承关系

 

Final 关键字作用,父类方法不想被子类重写,可以用此关键字

 

继承和组合  继承是表达 is a的关系, 组合表达的是 has a 的关系 (继承破环了 封装)

posted @ 2016-09-13 14:16  TomLiuxin  阅读(147)  评论(0编辑  收藏  举报