java入门基础知识和面向对象

自己整理的笔记,上一篇做了个思维导图,这一篇有详细的代码。

变量

变量是用来命名一个数据的标识符。

public class variables {
	public static void main(String[] args) {
		/*
		 * 定义变量:
		 * int 表示数据类型
		 * num 表示标识符(变量名)
		 * =   表示赋值操作符
		 * 5   表示数据类型的初始化值
		 * ;   表示改行结束,执行下一行
		 */
		int num = 5;
		System.out.println("打印变量: "+num);
	}
}

java数据类型

变量的数据类型决定了该变量可以存储什么样的值。

基本数据类型

在这里插入图片描述

public class BasciDataType {
	public static void main(String[] args) {
		/*
		 * 整数型
		 */
		// 1. byte 长度为8位
		byte a = 1;
		// 2. short 长度为16位
		short b = 100;
		// 3. int 长度为32位
		int c = 32;
		// 4. long 长度为64位
		long d = 600;

		/*
		 * 浮点型
		 */
		// 5. float
		float e = 54.321; //错误(类型不匹配:不能从double转换为float)
		float e1 = 54.321f;
		// 6. double
		double f = 54.321;
		
		/*
		 * 字符型
		 */
		// 7. char 只能存放一个字符
		char g1 = 'java';//只能存放一个字符
		char g2 = 'j';
		char g3 = '北';
		
		/*
		 * 布尔型
		 */
		// 8. boolean 
		boolean flag = false;
		boolean flag2 = true;
	}
}

引用数据类型

String就是常用的引用数据类型。

public class ReferenceDataType {
	public static void main(String[] args) {
		/*
		 * 引用数据类型 String
		 */
		String name = "张三";
		System.out.println(name);
	}
}

标识符命名规则

1)标识符由字母,数字,下划线,美元符号组成
2)标识符由字母,下划线,美元符号开头不能以数字开头
3)标识符不能使用关键字
4)区分大小写,遵循驼峰命名法

变量的作用域

变量名作用域
变量声明在类下面叫做属性,成员变量成员变量的作用域从声明位置开始的整个类
变量声明在方法上叫做参数作用域为该方法内的所有代码,其他方法不能访问改参数,类里也不能访问该参数
变量声明在方法内叫做局部变量作用域为声明开始位置,到所处代码块结束位置成员变量和局部变量同名时,局部变量优先

变量final

当一个变量被final修饰的时候,该变量只有一次赋值的机会。

		final int a;
		final double PI=3.14;
		//System.out.println(a);//没有初始化,必须进行赋值
		System.out.println(a=5);
		System.out.println(PI);

控制流程

条件判断

/*
 * 流程控制 if, if else, else if
 */
public class FlowControl {
	public static void main(String[] args) {
		int a = 3;// 声明一个成员变量
		// if
		if (a > 1) {
			System.out.println("Yes");
		}
		// if else
		if (a < 1) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
		// else if
		if (a == 1) {
			System.out.println("1");
		} else if (a == 2) {
			System.out.println("2");
		} else if (a == 3) {
			System.out.println("3");
		}
	}
}

switch 结构

/*
 * switch 结构
 */
public class Switch {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请选择: \n1 \n2 \n3");
		int choice = input.nextInt();//用户输入一个整数
		switch (choice) {
			case 1:
				System.out.println(1);
				break;//每个表达式之后都应该执行break 
			case 2:
				System.out.println(2);
				break;
			case 3:
				System.out.println(3);
				break;
			default:
				System.out.println("Wroing input!");
				break;
		}
	}
}

switch 练习: 用户输入月份后,然后使用switch 判断并输出季节

/*
 * switch 结构
 */
public class Switch {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入月份: ");
		int month = input.nextInt();//用户输入月份
		switch (month) {
			case 3: case 4: case 5:
				System.out.println("春天");
				break;//每个case之间没有break,程序会继续执行下去
			case 6: case 7: case 8:
				System.out.println("夏天");
				break;
			case 9: case 10: case 11:
				System.out.println("秋天");
				break;
			case 12: case 1: case 2:
				System.out.println("冬天");
				break;
			default:
				System.out.println("Wroing input!");
				break;
		}
	}
}

循环语句

循环结构组成:
	1. 循环变量初始化
	2. 循环条件
	3. 循环迭代(自增/自减)
例子:
	1. int i = 0;//次数初始值为0
	2. 循环条件 //while(i < 10)
	3. 循环迭代(自增/自减)//i++; i--;

while循环

  • 用户输入一个整数,使用while计算这个整数的阶乘
public class WhileTest {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("输入一个整数: ");
  int num = input.nextInt();
  int factorial = 1;//阶乘的最终结果为1
  int i = 0;
  while(i < num) {
   factorial = factorial*(num-i);// N的阶乘等于 N* (N-1) * (N-2) * ... * 1
   i++;
  }
  System.out.println("这个数的阶乘为: "+factorial);
 }
}

do-while循环:先执行再判断条件,至少会执行一次

  • 计算100以内的偶数和
public class DoWhileTest {
 public static void main(String[] args) {
  int num = 100; 
  int sum = 0; 
  int i = 0;//1).循环变量初始化
  do {
   if (i % 2 == 0) {
    //System.out.println(i);//打印一百以内的偶数
    sum += i;//sum=sum+i;
   }
   i++;//3).循环变量迭代
  } while (i <= num);//2).循环条件
  System.out.println("100以内的偶数和是: " + sum);
 }
}

for循环

/* for 循环练习:
  天朝有一个乞丐姓洪,去天桥要钱 
  第一天要了1块钱 
  第二天要了2块钱 
  第三天要了4块钱 
  第四天要了8块钱 
  以此类推 问题: 洪乞丐干10天,收入是多少?
*/
public class ForLoop {
 public static void main(String[] args) {
  int money=1;
  System.out.println("第1天要了1块钱");
  for (int i = 2; i <= 10; i++) {
   money*=2;
   System.out.println("第"+i+"天要了"+money+"块钱");
  }
 }
}

跳转语句

continue多用于循环,跳出本次循环,不执行后面的代码,继续下次循环

public class Continue {
 public static void main(String[] args) {
  //如果是双数,后面的代码不执行,直接进行下一次循环
  
  for (int i = 0; i <= 10; i++) {
   if(i%2==0) {
    continue;//不执行后面任何代码,直接跳进下一次循环
   }
   System.out.println("单数有: "+i);
  }
 }
}

continue练习题:

  • 打印 1-100 之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉
public class Continue {
 public static void main(String[] args) {
  for (int i = 0; i <= 100; i++) {
   if (i%3==0||i%5==0) {
    continue;//结束本次循环,继续下次循环
   }
   System.out.println("不是3/5的倍数的数: "+ i);
  }
 }
}

break直接结束循环

public class Break {
 public static void main(String[] args) {
  //如果是双数,直接结束循环
  for (int i = 0; i <= 10; i++) {
   if(i%2==0) {
    break;//遇到break,之间跳出循环
   }
   System.out.println("单数有: "+i);
  }
  System.out.println("test");//break之后,会执行方法体内其他命令
 }
}

数组

数组是一个固定长度的,可以存储同一数据类型的容器。

public class ArrayTest {
 public static void main(String[] args) {
  //1.声明数组
  int[] a;
  int a1[]; 
  
  //2.创建数组
  a1=new int[5];//创建数组的时候,要指明数组的长度
  
  //3.声明的同时,指向一个数组
  int[] a2=new int[3];
  
  //4.访问数组
  a2[0]=0; //数组下标从0开始
  a2[1]=1;
  a2[2]=2;
  a2[3]=3;//数组长度为3,这里实际长度为4,所以会产生数组下标越界异常
  
  System.out.println(a2.length);
 }
}

在一组长度为5的数组里,找到最小值和最大值.

public class ArrayTest {
 public static void main(String[] args) {
  int num[] = new int[] {12,13,45,4,89};  
  int min=num[0];//设定最小值
  int max=num[0];//最大值
  for (int i = 0; i < num.length; i++) {
   System.out.println(num[i]);
   if(min>=num[i]) {
    min=num[i];
   }else if(max<=num[i]) {
    max=num[i];
   }
  }
  System.out.println("最小值是: "+min);
  System.out.println("最大值是: "+max);
 }
}

Arrays 针对数组的工具类

public class ArrayTest {
 public static void main(String[] args) {
	  int[] a = new int[] { 3, 11, 7, 8, 9 };
	  int[] a1 = new int[] { 3, 11, 7, 8, 9 };
	  int[] a2 = new int[] { 6, 8, 9 };
	  
	  // 1. equals的用法:比较两个数组是否相等
	  Arrays.equals(a, a1);
	  System.out.println(Arrays.equals(a, a1));
	  System.out.println(Arrays.equals(a1, a2));
	  // 2. sort用法:将数组按照升序排列
	  Arrays.sort(a);
	  System.out.println("\na升序排列: ");
	  for (int i : a) {
	   System.out.print(i + " ");
	  }
	  // 3. toString用法:将一个数组换成一个字符串
	  String result = Arrays.toString(a);
	  System.out.println("\n" + result);
	  // 4. fill用法:把数组所有元素都赋值为val
	  Arrays.fill(a, 30);
	  System.out.println(Arrays.toString(a));
	  // 5. copyOf用法:把数组Array复制一个长度是length新数组
	  int[] a4 = new int[] { 10, 40, 40, 50, 60 };
	  int[] a5 = Arrays.copyOf(a4, 8);
	  System.out.println(Arrays.toString(a5));
	  // 6. binarySearch用法:查找val在数组array中的下标
	  Arrays.sort(a4);
	  int num = Arrays.binarySearch(a4, 60);
	  System.out.println(num);
	   }
	  }

类和对象

类是具有相同属性和方法的一组对象的集合。
对象是用来描述客观事物的一个实体,有一组属性和方法构成。

继承

继承是面向对象编程的三大特性之一

/*
 * 物品类:父类
 */
public class Item {
 //类的属性
 public String name;
 public int price;
}
/*
 * 武器类:子类
 */
public class Weapon extends Item{
 //使用extends关键字实现继承
 //类属性
 int damage;
}
public class WeaponTest {
 public static void main(String[] args) {
  //创建sword对象
  Weapon sword = new Weapon();
  sword.damage = 600; //调用weapon类的属性
  sword.name = "剑";//name的属性是从Item父类继承过来的,不需要在子类中再定义,直接调用
  sword.price = 500;//price属性也是从Item类继承来的,可直接调用
 }
}

方法重载

方法重载就是在同一个类中,可以有多个同名的方法,但参数类型或者参数列表个数不同。
方法重载练习:

  • 在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。
/*
 * 方法重载练习:
 * 在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。
 */
public class Test {
 public static void main(String[] args) {
  Test test = new Test();
  System.out.println("这两个数的和是: "+test.add(15, 15));
  System.out.println("这三个小数之和是: "+test.add(1.5, 1.5, 1.5));
 }
 
 //求两个整数的方法 add()
 public int add(int a,int b) {
  return a+b;
 }
 
 //求三个小数之和的方法 add()
 public double add(double a,double b,double c) {
  return a+b+c;
 }
}

构造方法

类创建对象的过程是实例化。
实例化是通过构造方法实现的。每个类都有默认的无参构造方法。构造方法名要与类名一致。

构造方法练习:

  • 设计一个立方体类Box,定义三个属性,分别是长,宽,高。定义二个方法,分别计算并输出立方体的体积和表面积
public class Box {
 //成员变量(属性)
 double length;//长度
 double width;//宽度
 double height;//高度
 
 //无参构造方法(默认)
 public Box() {
  
 }
 //有参构造方法
 public Box(double length,double width,double height) {
  this.length=length;
  this.width=width;
  this.height=height;
 }
 
 //打印体积
 public double printVolume() {
  return length*width*height;
 }
 
 //打印面积
 public double printArea() {
  return 2*(length*width+length*height+width*height);
 }
 
 public static void main(String[] args) {
  //创建一个Box的对象(实例化一个Box的对象)
  Box box = new Box(1.5,1.6,1.7);//有参构造用于对象数据初始化
  System.out.println("立方体的体积为: "+box.printVolume());
  System.out.println("立方体的表面积为: "+box.printArea());
 }
}

类属性&类方法(静态属性&静态方法)

当一个被static修饰时,就叫做类属性,又叫做静态属性,当一个属性是类属性时,所有的对象都共享一个值
当一个方法被static修饰时就是类方法,直接通过类名就可以访问的方法。

public class Test {
 //静态属性(类属性)
 static int MAX = 999;
 
 //静态方法(类方法)
 public static void method1() {
  System.out.println("这里是静态方法!");
 }
 
 //成员方法
 public void test1() {
  System.out.println("这里是成员方法!");
 }
 
 public static void main(String[] args) {
  int num = Test.MAX;//使用类名访问类属性,不需要创建对象
  System.out.println("静态属性的值是: "+num);
  
  Test.method1();//直接通过类名调用
  
  Test test=new Test();
  test.test1();//需要创建一个对象才可以调用
 }
}

接口

接口练习:

  • 编写一个接口:InterfaceA,只含有一个方法int method(int n);
  • 编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算1到n的和;
  • 编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算n的阶乘(n!);
  • 编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现接口的类。
public interface InterfaceA {
 int method(int n);//默认public abstract
}
public class ClassA implements InterfaceA{
 @Override
 //实现接口内所有方法
 public int method(int n) {
  // 计算1到n的和
  int sum = 0;
  for (int i = 0; i <= n; i++) {
   sum+=i;
  }
  return sum;
 }
}
public class ClassB implements InterfaceA{
 @Override
 public int method(int n) {
  // 计算n的阶乘(n!) 
  int factorial = 1; 
  for (int i = 0; i < n; i++) {
   factorial= factorial*(n-i);
  }
  return factorial;
 }
}
public class Test {
 public static void main(String[] args) {
  InterfaceA classA = new ClassA();//向上转型
  System.out.println("1到5的和是: "+classA.method(5));
  
  InterfaceA classB = new ClassB();//向上转型
  System.out.println("5的阶乘是: "+classB.method(5));
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值