Java学习教程02:基础知识从0到1

1 基础知识

使用Java的开发IDE是Eclipse,先介绍几个基本的操作:

  • 切换工作空间:File --> Switch Workspace
  • 自动编译:勾选 Project --> Bulid Automatically
  • 视图相关的设置:Windows --> Show View
  • 恢复默认视图:Windows --> Perspective --> Reset Perspective
  • 首选项:Windows --> Perferences,修改显示字体字号等配置设置
  • 安装插件:Help --> Install New Software

创建java项目:

  • 创建java工程:File --> New --> Java --> Java Project,默认参数,Finished
  • 创建java包:相当于名称空间,在硬盘上的体现为一个目录;如果不小心把包的名字写错了,右键–>refacter–>rename,进行修改
  • 创建java类:可以勾选public static void main(String[] args),程序会自动添加入口函数

项目调试:

  • F11:进入调试界面
  • F7:执行本行代码
  • F5:步入函数内部
  • F6:步出函数

1.1 入门demo

类由属性和方法构成,有关键字class修饰

public class FirstClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Boy xm = new Boy();
		xm.getMsg();
		
		Boy tom = new Boy();
		tom.name = "Tom";
		tom.age = 30;
		tom.getMsg();
	}
	
	public static class Boy{
		String name = "XiaoMing";
		int age = 18;
		public void getMsg() {
			System.out.println(name + "," + age);
		}
		public void eat() {
			System.out.println("eat");
		}
	}
}

1.2 基本数据类型

为什么要有数据类型?

我们要使用变量来存数据,不同的数据需要的内存空间也不相同;我们需要定义不同的数据类型满足不同数据存储的要求。

四类八种数据类型:boolean、char、byte、short、int、long、double、float

每个基本数据类型都有一个封装类

package excise01;
//本示例讲述基本数据类型及其封装类
public class Demo02 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//四类八种数据类型:
		//--整数类
		byte b = 127; //最大取到127
		short s = 32767; //最大32768
		int i = 100000;
		long l = 1000000000;
		//--浮点数类型
		double d = 3.14;//浮点数默认数据类型
		float f = 3.009F; //单精度浮点数要加F
		//--字符类
		char c = 'A'; //65
		//--布尔类型
		boolean flag = true;
		
		//----------基本类型的封装类[首字母大写]------------------
		System.out.println("Type:  Min  Max  Size");
		System.out.println("Byte:" + Byte.MIN_VALUE + "," + Byte.MAX_VALUE + ","+Byte.SIZE);
		System.out.println("Short:" + Short.MIN_VALUE + "," + Short.MAX_VALUE + ","+Short.SIZE);
		System.out.println("Integer:" + Integer.MIN_VALUE + "," + Integer.MAX_VALUE + ","+Integer.SIZE);
		System.out.println("Long:" + Long.MIN_VALUE + "," + Long.MAX_VALUE + ","+Long.SIZE);
		System.out.println("Double:" + Double.MIN_VALUE + "," + Double.MAX_VALUE + ","+Double.SIZE);
		System.out.println("Float:" + Float.MIN_VALUE + "," + Float.MAX_VALUE + ","+Float.SIZE);
//		Type:  Min  Max  Size
//		Byte:-128,127,8
//		Short:-32768,32767,16
//		Integer:-2147483648,2147483647,32
//		Long:-9223372036854775808,9223372036854775807,64
//		Double:4.9E-324,1.7976931348623157E308,64
//		Float:1.4E-45,3.4028235E38,32
	}
}

1.3 运算符

算术运算: 加、减、乘、除、余【+, -, *, /, %】;使用Math.Random()可以生成随机数

关系运算: >, >=, <, <=, ==, !=

逻辑运算:& | !

赋值运算:=,+=, -=, /=,

自增自减:前缀式,先加(减)再用;后缀式,先用再加(减)

三元运算符:x>y ? x:y;

1.4 字符串

定义在java.lang.String包中

初始化两种方式:构造函数初始化、直接赋值初始化

字符串拼接的两种方法:concat、+

字符串的格式化:%d, %s, %c, %.2f

常用方法:charAt、length、substring、trim、indexOf

public static void testStr()
	{
		//1. 初始化方法
		String s1 = "Tom, ";
		String s2 = new String("Kite"); //有很多重载版本
		
		//2. 字符串拼接
		System.out.println(s1+s2);
		System.out.println(s1.concat(s2));
		
		//3. 格式化字符串
		String name = "Ray";
		int age = 28;
		char sex = 'M';
		double weight = 37.98;
		//formats是静态方法,java1.5以上才支持这个方法,如果报不匹配错误,点击Project --> Properties --> Java Compiler --> Complier compliance level后边设置为1.5以上版本
		String info = String.format("姓名:%s, 年龄:%d, 性别:%c, 体重:%.2fkg",name, age, sex, weight);
		System.out.println(info);
		
		//4. charAt 查找某一位置字符
		for(int i = 0; i < info.length(); i++)
		{
			if(info.charAt(i) == ',')
			{
				System.out.print(';');
			}else
			{
				System.out.print(info.charAt(i));
			}
		}
		System.out.println();
		//5 substring 提取子串
		System.out.println(info.substring(0, 6));
		
		//6 trim 去掉首尾空格
		System.out.println("       首都:北京    ".trim());
		
		//7 indexof 子串或字符首次出现的位置
		System.out.println(info.indexOf('8'));
	}

1.5 数组

数组长度大小固定,保存到额数据类型相同

初始化:直接初始化,逐个元素的初始化

遍历数组:for

做输入和输出参数

Arrays工具类:sort排序;binarySearch二分查找,需先排序;

	public static void testArray()
	{
		//1. 数组的初始化
		int[] arr1 = {0,1,2,3,4,5};
		System.out.println(arr1.length);
		
		int[] arr2 = new int[5];
		arr2[2] = 89;
		arr2[4] = 92;
		//2 输出数组
		for(int i : arr2)
		{
			System.out.print(i + ",");
		}
		System.out.println();
		//3 工具类
		Arrays.parallelSort(arr1);
		System.out.println(Arrays.binarySearch(arr1,10)); //二分查找必须先排序		
	}

1.6 流程控制

java的程序是按顺序执行的,遇到函数处;进入函数执行,执行完毕后,返回到函数处;继续执行

循环语句

- for(迭代变量初值;终止条件;迭代变量变化规律)
- while循环,do...while至少执行一次
- break 跳出循环;continue终止本次循环

分支语句

  • if(条件){执行分支}; if(){…} else if(){…}else{}
  • switch(key) {case 1: …; break; case 2: …; break; default: …; break}
	public static void test()
	{
		int[] arr = {5,1,5,4,5,3,7,3,2};
		//1 for循环
		//普通for循环
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + ",");
		}
		System.out.println();
		//增强型For循环,foreach
		for(int i :arr)
		{
			System.out.print(i+ ",");
		}
		System.out.println();
		
		//2 while
		int sum = 0, tmp = 0;
		while(tmp<100) {
			sum+=tmp;
			tmp++;
		}
		System.out.println(sum);
		do {
			System.out.println("执行...");
			tmp++;
		} while (tmp <= 101);

	}

2 面向对象

2.1 类和对象

类是抽象的,对象是具体的

明确几个概念:类、对象、类变量与方法、实例变量与方法、构造函数

class Person{
	String name;//实例变量
	int age;//实例变量
	static int pid = 30;//类变量
	//类方法
	static void m1() {
		System.out.println("m1......");
	}
	//实例方法
	public void work() {
		System.out.println("正在工作...");
	}
	//实例方法
	public void eat() {
		System.out.println("正在吃饭...");
	}
	//构造方法,不定义默认无参构造,若自定义不会增加午餐构造,需自定义
	Person(String Name, int Age){
		this.name = Name;
		this.age = Age;//this表示当前实例对象
	}
	Person(){}
}

2.2 接口

interface定义;没有方法体,空方法;接口为了更好的实现的程序的抽象

	interface Animal{
		public void eat();
		public void sleep();
		public void play();
	}
	//点击类名称行首位置,自动生成未实现的方法
	class Monkey implements Animal{
		@Override
		public void eat() {
			// TODO Auto-generated method stub
			System.out.println("Monkey eating...");
		}
		@Override
		public void sleep() {
			// TODO Auto-generated method stub
			System.out.println("Monkey sleeping...");
		}
		@Override
		public void play() {
			// TODO Auto-generated method stub
			System.out.println("play monkey...");
		}	
	}

一个类可以实现多个接口,java不支持多重继承

	interface Fly{
		public void flying();
	}
	interface Fish{
		public void fishing();
	}
	class flyFish implements Fly, Fish{

		@Override
		public void fishing() {
			// TODO Auto-generated method stub
			System.out.println("游泳");
		}

		@Override
		public void flying() {
			// TODO Auto-generated method stub
			System.out.println("飞翔");
		}
		
	}

接口之间也可以继承

	//接口也可以继承
	interface player{
		public void play();
	}
	interface playVideo extends player{
		public void playSound();
	}
	static class TV implements playVideo{

		@Override
		public void playSound() {
			System.out.println("播放声音...");
		}

		@Override
		public void play() {
			System.out.println("播放...");		
		}	
	}

2.3 继承

基本语法:

  • 除了私有成员都可以被继承;

  • Object是java中的顶级父类,任何自定义类都是Object的子类

  • Super代表的是父类;this为当前类的实例

  • 使用extends关键字

  • 方法重载:方法名称相同,参数个数或类型不相同;

  • 方法覆盖:在继承关系中,override

2.4 多态的原理

  • 使用关键字implements,一个类可以实现多个接口,逗号隔开
  • 向上类型转换:Cat --> Animal;方法的动态绑定,一个行为多种状态
package csdn.ghz;
//多态演示示例
public class Demo08 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PersonRay XiaoWang = new PersonRay();
		Animal myCat = new Cat();
		XiaoWang.play(myCat);
		Animal myDog = new Dog();
		XiaoWang.play(myDog);
	}

	public static class PersonRay{		
		public PersonRay() {
			super();
			// TODO Auto-generated constructor stub
		}

		public void play(Animal ani) {
			ani.eat();
			ani.sleep();
		}
	}
	
	interface Animal {
		public void eat();
		public void sleep();
	}
	
	public static class Cat implements Animal{
		@Override
		public void eat() {
			System.out.println("Cat eating...");
		}
		@Override
		public void sleep() {
			System.out.println("Cat sleeping...");
		}		
	}
	
	//如果想养一个狗,只需要增加狗对Animal的实现即可;无需修改Person的代码
	public static class Dog implements Animal{

		@Override
		public void eat() {
			System.out.println("dog eating...");
		}

		@Override
		public void sleep() {
			System.out.println("dog sleeping...");
		}
		
	}
}

3 简单的酒店管理系统示例

需要三个类:Customers、Room和HotelManager类

3.1 Customers类

package com.csdn;

public class Customers {

	private int cid;
	private String name;
	private Room room;
	private int days;
	
	public Customers() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Customers(int cid, String name, Room room, int days) {
		super();
		this.cid = cid;
		this.name = name;
		this.room = room;
		this.days = days;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}
	
}

3.2 Room类

package com.csdn;

public class Room {
	private String no;
	private int isBooked;
	
	
	//生成无参构造方法
	//右键-->Source-->Generate Constructor from superclass
	public Room() {
		super();
		
	}
	//生成全参构造方法
	//右键-->Source-->Generate Constructor using field
	public Room(String no, int isBooked) {
		super();
		this.no = no;
		this.isBooked = isBooked;
	}
	//右键--》Source-》Generate Setters and getters
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public int getIsBooked() {
		return isBooked;
	}
	public void setIsBooked(int isBooked) {
		this.isBooked = isBooked;
	}
	
	public String toString()
	{
		return this.no + "," + (this.isBooked == 1 ? "已入住" : "未入住");
	}
	
}

3.3 HotelManager类

package com.csdn;

import java.util.ArrayList;

public class HotelManager {
	ArrayList<Room> roomlist = new ArrayList<Room>();
	public void initRoom() {

		Room r101 = new Room("101", 0);
		Room r102 = new Room("102", 0);
		Room r103 = new Room("103", 0);
		Room r104 = new Room("104", 0);
		Room r105 = new Room("105", 0);
		
		Room r201 = new Room("201", 0);
		Room r202 = new Room("202", 0);
		Room r203 = new Room("203", 0);
		Room r204 = new Room("204", 0);
		Room r205 = new Room("205", 0);
		
		roomlist.add(r101);
		roomlist.add(r102);
		roomlist.add(r103);
		roomlist.add(r104);
		roomlist.add(r105);
		
		roomlist.add(r201);
		roomlist.add(r202);
		roomlist.add(r203);
		roomlist.add(r204);
		roomlist.add(r205);	
	}
	
	//入住
	public void checkIn(String roomNo)
	{
		for(Room room : roomlist) {
			if(room.getNo().equals(roomNo)) {
				room.setIsBooked(1);
				break;
			}
		}
	}
	
	//退房
	public void checkOut(String roomNo)
	{
		for(Room room : roomlist) {
			if(room.getNo().equals(roomNo)) {
				room.setIsBooked(0);
				break;
			}
		}
	}

	public void show() {
		for(Room room : roomlist) {
			System.out.println(room);
		}
	}
}

3.4 测试代码

package com.csdn;

import java.util.Scanner;

public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String menu = "请选择:\n 1: 入住; \n2: 退房;\n3: 查房\n4: 退出系统";
		HotelManager hm = new HotelManager();
		hm.initRoom();
		System.out.println(menu);
		Scanner s = new Scanner(System.in);
		while(true) {
			if(s.hasNext()) {
				int choice = s.nextInt();
				switch(choice) {
				case 1: 
					System.out.println("请输入入住房间号:");
					Scanner s1 =  new Scanner(System.in);
					if(s1.hasNext()) {
						String no = s1.next();
						hm.checkIn(no);
						System.out.println("入住成功");
					}
					//s1.close();
					break;
				case 2:
					System.out.println("请输入退房房间号:");
					Scanner s2 =  new Scanner(System.in);
					if(s2.hasNext()) {
						String no = s2.next();
						hm.checkOut(no);
						System.out.println("退房成功");
					}
					//s2.close();
					break;
				case 3:
					hm.show();
					break;
				case 4:
					System.out.println("good bye");
					//s.close();
					System.exit(0);
					break;
				default:
					break;
				}
			}
		}		
	}
}

支持作者分享,欢迎扫码关注,一起进步
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值