2015年5月16日1:private 1): private私有属性 当成员被私有化之后 那就只能提供方法来进行访问 2):private私有化的属性只在本类中有效 也就是这个声明变量的这个类 比如:
那么private到底有什么用呢?public class PersonPrivate { public static void main(String[] args) { Demo a = new Demo(); //a.age = 19; a.setAge(19);//方法访问private私有属性 a.speak(); } } class Demo { private int age;//私有属性 不可以访问 //提供方法来访问这个age参数 public void setAge(int age) { this.age = age; } public void getAge() { return this.age;//在实际开发过程中 一般都有set和get方法 这是一个好的习惯! } void speak() { System.out.println("age = " + age); } }
如下:
1):比如我有个个人信息类,把密码写成praviate String password;
我在类中只有两个方法让类外用public void set(),和public String get()方法来得到这个密码,而且我在set中调用了md5(字符加密方法来set这个密码,比如密码是123我可以把它转换为别人看不懂的32位字符),get(md5的解码方法)方法来解码,这样我保存再数据库的字符就是别人看不懂的密码,保护了客户信息,这样你只能用get()方法来得到这个解码过的密码,而真正的密码确实谁都看不懂的32位码,如果密码是public 那么只要new 这个类就可以得到这个被加过密的真正密码,这是一个很恐怖的事,你的加密的规则就会被知道,用户密码就非常容易泄露。其他资源也是一样。
__________________________________________________________________________________________________________
2):private是只能自己类访问的,不然的话其它的程序都可以肆意更改private变量了,可以增加安全性能。
比如说一个个人存款的javabean,private double myaount(银行存款变量),那么这个变量对于其它类是不可见的,如果你要调用的时候只能用这个类的get方法来得到(你也可以不写get方法),这样就约束了人为的,或者意外的其它不安全的访问更改这个变量的因素。(如果访问这个属性的途径太多那么造成的安全隐患是显而易见的)所以一般就把类封装起来只留下public double getMyaount()的方法来得到这个变量。public void setMyaount()方法来设置这个变量
__________________________________________________________________________________________________________
3):之所以对外提供访问方式 是因为可以再访问方式内添加逻辑语句结构对访问的数据进行操作,提高代码的健壮性!
4):封装原则: 把属性隐藏,提供公共方法进行访问!
2: 构造代码块: 代码如下:public class CodeBlocks { int age; public CodeBlocks(){} public CodeBlocks(int age) { this.age = age; System.out.println("我是构造器"); } /* 构造代码块。 作用:给对象进行初始化,对象一旦建立就运行,而且优先于构造器函数运行 和构造函数的区别。 构造函数是给对应的对象进行初始化,构造代码块是给所有的对象进行初始化 */ { this.age = 11; System.out.println("我是代码块"); } public static void main(String[] args) { CodeBlocks b = new CodeBlocks(); b.age = 12; System.out.println(b.age); System.out.println(new CodeBlocks().age);//11 } }
构造代码块。
作用:给对象进行初始化,对象一旦建立就运行,而且优先于构造器函数运行
和构造函数的区别。
构造函数是给对应的对象进行初始化,构造代码块是给所有的对象进行初始化
3:this语句: 代码如下:
public class ThisApplyTwo { public static void main(String[] args) { Car c1 = new Car("宝马", 12, 200); System.out.println(c1.getName() + "->" + c1.getNum() + "->" + c1.getPrice()); } } class Car { String name; int num; int price; Car(String name) { this.name = name; } Car(String name, int num) { this(name);//如下就可以调用构造方法 this.num = num; } Car(String name, int num, int price) { this(name, num);//这也是一样 this.price = price; } public String getName() { return this.name; } public int getNum() { return this.num; } public int getPrice() { return this.price; } }
this语句用于构造函数之间互相调用!但是必须位于第一行!
4:static关键字 特点: (1)一旦成员变量被static修饰 那么在内存里面就不在堆里面了 而是在栈里面了 可以节约空间 (2)而且调用的方式也可以是类名.变量名来调用 (3)优先于对象而存在 优先于非静态变量存在 (4)被所有对象共享 (5)static变量随着类的消失而消失 但是实例变量随着对象的消失而消失 这也是它的缺点之一 生命周期长, 占内存。 static的运用:
然后在调用这个类的方法, 因为是static修饰的 所以可以这样来调用:import java.util.*; public class ArrayTool { public static int getMax(int[] arr) { int max = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] > arr[max]) max = i; } return arr[max]; } public static int getMin(int[] arr) { int min = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[min]) min = i; } return arr[min]; } public static void Sort(int[] arr) { for (int i = 0;i < arr.length-1; i++) { int j = i; for (int k = j + 1; k < arr.length; k++) if (arr[j] > arr[k]) j = k; if (j != i) swap(arr, i, j); } } public static void swap(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } public static void PrintArray(int[] arr) { System.out.println(Arrays.toString(arr)); } }
运行结果:public class StaticTool { public static void main(String[] args) { int[] arr = {1, 5, 8, 4, 9, 12, 45, 3, 0, 67, -9}; System.out.println("max = " + ArrayTool.getMax(arr)); ArrayTool.PrintArray(arr); ArrayTool.Sort(arr); ArrayTool.PrintArray(arr); } }
max = 67 [1, 5, 8, 4, 9, 12, 45, 3, 0, 67, -9] [-9, 0, 1, 3, 4, 5, 8, 9, 12, 45, 67]
-------------------------------------------------------------------------------------------------------------------
由于前面忘掉太多JAVA基础知识点, 特地回顾一下, 记录如上!
JAVA基础知识块整理
最新推荐文章于 2025-05-07 15:32:02 发布