JAVA----关键字用法
2015-10-28 17:33
543 查看
this
使用注意:
1.代表所属函数的调用者对象。 2.如果存在同名的成员变量和局部变量时,在方法内部默认是访问局部变量(java编译器采用“就近原则”),但可以用this访问成员变量。 3.在一个构造函数中可以访问另一个构造函数,但是调用语句必须在该构造函数的第一个语句,另外这两个函数之间不能互相调用。 4.如果在一个方法中访问一个变量时,该变量只有成员变量时,java编译器会在该变量前加this。
代码:
class People { int id; String name; int age; public People(int id,String name, int age) { this(age); this.id = id; this.name = name; } public People(int age) { this.age = age; } public void compara(People p1){ if(this.age > p1.age) System.out.println(this.name + " big"); else if(this.age < p1.age) System.out.println(p1.name + " big"); else System.out.println( "equals"); } } class Demo1 { public static void main(String[] args) { People a = new People(11,"Tom",48); People b = new People(12,"Marry",30); a.compara(b); } }
static
static修饰成员变量:
访问方式:
对象.属性名 或 类名.属性名((比较常用)) 对象.函数名() 或 类名.函数名()((比较常用))
使用注意:
1.static 修饰成员变量时,该成员变量的数据属于共享数据。 2.非静态成员变量只能用对象访问。 3.静态函数能直接访问静态的成员,不能直接访问非静态成员,但如果静态函数中存在对象,则可以访问非静态成员。 4.静态函数不能含有this和super关键词。 5.非静态函数既能调用静态成也能调用非静态成员。 **静态成员变量的数据优先于对象存在**。
class ArrayTool { public static String ToString(int[] arr) { String aa=""; for(int i=0; i<arr.length; i++) { if(i==0) aa+="["+arr[i]; else if(i==arr.length-1) aa+= arr[i] +"]"; else aa +=" "+ arr[i] + " "; } return aa; } public static void Sort(int[] arr) { int len=arr.length; for(int i=0; i<len; i++) { for(int j= i+1; j<len-1; j++) { if(arr[i]>arr[j]) { int temp; temp= arr[i]; arr[i]= arr[j]; arr[j]=temp; } } } } } class Demo2 { public static void main(String[] args) { int[] arr = {1, 6, 9, 3, 5}; ArrayTool.Sort(arr); System.out.println("Array" + ArrayTool.ToString(arr)) ; } }
super
定义:
代表父类空间的引用;
作用:
1.子父类存在同名的成员时,在子类中默认是访问子类的成员,但可以通过super访问父类成员。 2. 创建子类对象时,默认会调用父类无参的构造方法,可以用super指向父类的构造方法。
注意事项:
1. 如果在子类的构造方法上没有指定调用父类的构造方法时,java编译器会在子类的构造方法上加上super。 2. super关键字调用父类的构造函数时,该语句必须是子类构造函数的第一个语句。 3. super 和 this 不能出现在同一个构造函数中。
final
作用:
final 关键字修饰一个基本类型变量时,该变量不能重新赋值,第一次的值为最终的。 fianl 关键字修饰一个引用类型变量时,该变量不能重新指向新的对象。 final 关键字修饰一个函数时,该函数不能被重写。 fianl 关键字修饰一个类时,该类不能被继承。
常量的修饰符一般为: public static final
常量一般全部用大写字母表示,单词之间用下划线隔开
代码:
class Circle { double r; final double pi = 4.0; public Circle(double r) { this.r = r; } public void getArea() { System.out.println("圆形的面积" + r*r*pi); } } class Demo1 { public static void main(String[] args) { Circle c = new Circle(4.0); c = new Circle(5.0); c.getArea(); //test(c); } }
abstract
使用注意:
1.如果一个函数没有方法体,该函数必须用abstract修饰。 2.如果一个类有抽象函数,那么该类必须用abstract修饰。 3.如果一个非抽象的类继承了抽象的类,那么必须把抽象的方法全部实现。 4.抽象类不能创建对象。 5.抽象类可以存在非抽象方法和构造函数。 6.abstract不能与final ,static , private 一起修饰一个方法。
代码:
//用抽象类求矩形,圆形的面积和周长 abstract class MyShape { public abstract void getArea(); public abstract void getLength(); } class Rect extends MyShape { double x,y; public Rect(double x,double y) { this.x = x ; this.y = y ; } public void getArea() { System.out.println("Rect " + x*y); } public void getLength() { System.out.println("Rect " + 2*(x+y)); } } class Circle extends MyShape { double r; public static final double PI = 3.14; public Circle(double r) { this.r = r; } public void getArea() { System.out.println("Circle " + r*r*PI); } public void getLength() { System.out.println("Circle " + 2*r*PI); } } class Demo3 { public static void main(String[] args) { Circle ss = new Circle(4.0); ss.getArea(); ss.getLength(); Rect aa = new Rect(2,4); aa.getArea(); aa.getLength(); } }
instanceof
定义:
判断一个对象是否属于指定的类型
使用格式:
对象 instanceof 类别
使用前提:
判断的对象于指定的类型必须要存在继承或实现的关系
代码:
class Animal { String name; public Animal(String name) { this.name = name; } } class Dog extends Animal { public Dog(String name) { super(name); } public void bite() { System.out.println(name + "咬人"); } } class Mouse extends Animal { public Mouse(String name) { super(name); } public void dig() { System.out.println(name + "挖洞"); } } class Demo2 { public static void main(String[] args) { Dog ss = new Dog("dddddd"); System.out.println("狗是狗类吗"+ (ss instanceof Dog)); //true Animal dd = new Animal("ffff"); System.out.println("动物是狗类吗"+ (dd instanceof Dog)); //false } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统