本文共 3420 字,大约阅读时间需要 11 分钟。
本文对JAVA基础知识进行一个小总结:
一、基础变量类型
存储大小 例值 注释
byte 1byte 3 字节
int 4bytes 3 整数
short 2bytes 3 短整数
long 8bytes 3 长整数
float 4bytes 1.2 单精度浮点数
double 8bytes 1.2 双精度浮点数
char 2bytes ‘a’ 字符
boolean 1bit true 布尔值
注意:java中变量需要先声明后使用。
二、Java数组定义及其初始化
一维数组及其初始化
int[] arr0;
int[] arr1 = new int[100];
int[] arr2 = new int[]{1,2,3,4};
二维数组及其初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/************************/
int[][] arr0;
/************************/
int[][] arr1= new int[5][6];
for(int i = 0;i<5;i++){
for (int j = 0;j<6;j++){
arr1[i][j] = i+j;
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
/*************************/
int[][] arr2 = new int[][]{
{1,2,3},{4,5,6},{7,8,9}};for(int i = 0;i<2;i++){
for (int j = 0;j<3;j++){
System.out.print(arr2[i][j]+" ");
}
}
三、Java面向对象编程(OOP)之三大特性:封装、继承、多态
封装:将对象的属性及相关操作等组合在一起,以方便管理和使用。
封装一个“人类”:
1
2
3
4
5
6
7
8
9
10
11
12
class Human{
private int age;
Human(int value){
this.age = value;
}
public int getAge(){
return this.age;
}
public void setAge(int value){
this.age = value;
}
}
继承:为避免重复代码,通过继承来实现代码复用。
定义一个基类Human,然后定义一个类Man继承基类的属性和方法,并在子类中实现子类自己特有的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Human{
private int age;
Human(int value){
this.age = value;
}
public int getAge(){
return this.age;
}
public void setAge(int value){
this.age = value;
}
}
class Man extends Human{
Man(int age){super(age);};
public void Play(String item){
System.out.println("man always like "+item);
}
}
多态:主要通过方法重载、覆盖来实现多态特性。
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
public class Test
{
public static void main(String[] args)
{
Cup aCup;
BrokenCup aBrokenCup = new BrokenCup();
aCup = aBrokenCup; // upcast
aCup.addWater(10); // method binding
}
}
class Cup
{
public void addWater(int w)
{
this.water = this.water + w;
}
public void drinkWater(int w)
{
this.water = this.water - w;
}
private int water = 0;
}
class BrokenCup extends Cup
{
public void addWater(int w)
{
System.out.println("shit, broken cup");
}
public void drinkWater(int w)
{
System.out.println("om...num..., no water inside");
}
}
运行结果:
shit, broken cup
根据运行结果可以看出,Java可以根据当前状况,识别对象的真实类型,这叫做多态(polymorphism)。
四、面向接口编程
在设计模式的6大设计原则中,其中的两条原则是依赖倒置原则、接口隔离原则。
依赖倒置原则描述如下:
A.高层次的模块不应该依赖于低层次的模块,他们都应该依赖于抽象。
B.抽象不应该依赖于具体,具体应该依赖于抽象。
接口隔离原则描述如下:
简而言之就是不要建立臃肿的接口,接口中的方法应尽量少,一个类对另一个类的依赖应该建立在最小的接口上,以避免客户依赖(实现)不必要的接口。
依赖倒置原则告诉我们要面向接口编程;接口隔离原则告诉我们在设计接口的时候要精简单一。
在java中,接口(interface)使用方法如下:
1)以杯子为例,定义一个杯子的接口:
1
2
3
4
interface Cup {
void addWater(int w);
void drinkWater(int w);
}
我们可以在一个类的定义中实施接口,比如下面的MusicCup (可以播放音乐的杯子):
1
2
3
4
5
6
7
8
9
10
11
12
class MusicCup implements Cup
{
public void addWater(int w)
{
this.water = this.water + w;
}
public void drinkWater(int w)
{
this.water = this.water - w;
}
private int water = 0;
}
为了遵循接口隔离原则,我们需要尽可能的避免接口臃肿,因此,有时候,我们可能需要实现多个接口来达到某种编程目的。
java中实现多个接口的方法如下:
定义两个接口,水杯接口Cup,音乐播放接口MusicPlaer。通过同时实现这两个接口来实现一个“音乐水杯”。
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
interface Cup {
void addWater(int w);
void drinkWater(int w);
}
interface MusicPlayer {
void play();
}
//通过实现Cup接口和MusicPlayer接口来实现一个“音乐水杯”
class MusicCup implements MusicPlayer, Cup
{
public void addWater(int w)
{
this.water = this.water + w;
}
public void drinkWater(int w)
{
this.water = this.water - w;
}
public void play()
{
System.out.println("la...la...la");
}
private int water = 0;
}
参考文章:
http://java.tanzhouedu.net
转载地址:https://blog.csdn.net/weixin_34007963/article/details/114221940 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!