java初级笔记(翁恺男神
一、基础
1.类型转换
两个整数的运算结果只能是整数(自动向下取整
自动类型转换(由低变高
强制类型转换(从高到低
2.运算符优先级
正负号》乘除取余》加减连接》关系运算符》赋值
//所有的单目运算符优先级最高
* 简单应用
猜数游戏
package hello;
import java.util.Scanner;
public class GuessNum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a, count = 0, n = (int)(Math.random() * 100 + 1);//[0,1)
do {
a = in.nextInt();
if(n == a)
System.out.println("All right!");
else if(n > a)
System.out.println("so small");
else
System.out.println("so big");
count++;
}while(n != a);
System.out.println("the all times:" + count);
}
}
求最大公约数 by辗转相除法
package hello;
import java.util.Scanner;
public class CommonDiv {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt(), b = in.nextInt(), t;
while(b != 0) {
t = a % b;
a = b;
b = t;
}
System.out.println(a);
}
}
3.数组
what
定义
访问by下标
自动初始化为0 & 数组大小可以数组名.length
数组变量
不同于C,Java中可以进行数组变量之间的赋值
复制数组
只能一一拷贝
tips:遍历次数确定时,最合适的方式是for循环
for-each循环
where:适合于遍历场景,且和元素下标无关
4.字符
*简单应用之井字棋
package hello;
import java.util.Scanner;
public class Tic {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size = in.nextInt(), numOfX = 0, numOf0 = 0, x, result = 0;
int[][] a = new int[size][size];
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < size; j++) {
a[i][j] = in.nextInt();
}
}
for(int i = 0; i < a.length; i++) {//行
for(int j = 0; j < a[i].length; j++) {
if(a[i][j] == 0)
numOf0++;
else
numOfX++;
}
if(numOf0 == size || numOfX == size) {
result = 1;
break;
}
}
if(result != 1) {//列
numOf0 = numOfX = 0;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
if(a[j][i] == 0)
numOf0++;
else
numOfX++;
}
if(numOf0 == size || numOfX == size) {
result = 1;
break;
}
}
}
if(result != 1) {//对角线
numOf0 = numOfX = 0;
for(int i = 0; i < size; i++) {
if(a[i][i] == 0)
numOf0++;
else if(a[i][i] == 'X')
numOfX++;
}
if(numOf0 == size || numOfX == size) {
result = 1;
}
}
if(result != 1) {//反对角线
numOf0 = numOfX = 0;
for(int i = 0; i < size; i++) {
if(a[i][size - i - 1] == 0)
numOf0++;
else if(a[i][size - i - 1] == 'X')
numOfX++;
}
if(numOf0 == size || numOfX == size) {
result = 1;
}
}
if(result == 1)
System.out.println("Win!");
else
System.out.println("Lose!");
}
}
逃逸字符(转义字符
.运算符
对于Character类而言常用的有:
字符串变量
== vs equals
== 是否指向同一个字符串
equals() 字符串内容是否相同
字符串操作
注意不是对s直接修改,而是根据要求修改出新的字符串
字符串的长度
注意未初始化的字符串不能求长度,人家连个小兵都没有,你问手下小兵几许,很不礼貌的欸
得子串
寻找字符
5.函数
本地变量(形参
二、入门
1.类
类vs对象
对象变量是变量的管理者,而不是所有者。对象需要new出来
封装:把数据和对数据进行的操作放在一起
简单应用
package hello;
public class Time {
private int limit = 0, value = 0;
public Time(int limit) {
this.limit = limit;
}
public void increase() {
value++;
if(value == limit)
value = 0;
}
public int getValue() {
return value;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Time t = new Time(24);
while(true) {
t.increase();
System.out.println(t.getValue());
}
}
}
package hello;
public class Clock {
private Time h = new Time(24);
private Time m = new Time(60);
public void start() {
m.increase();
if(m.getValue() == 0)
h.increase();
System.out.printf("%02d:%02d\n", h.getValue(), m.getValue());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Clock c = new Clock();
while(true) {
c.start();
}
}
}
2.重载
在同一个作用域中,如果有多个函数的名字相同,但是形参列表不同(参数类型不同或参数个数不同),返回值类型可同也可不同,我们称之为重载函数。重载的函数是通过形参列表区分的,与返回值类型无关。函数重载其实是"一个名字,多种用法"的思想,不仅函数可以重载,运算符也可以重载。
3.封装的访问属性
private
public
friendly(即default
what: 成员变量和成员函数既没有public也没有private,前面是空的权限:被同一个包(package)里的其他人看到的。
4.泛型容器类
ArrayList 类是一个可以动态修改的数组
简单应用之记事本
package hello;
import java.util.ArrayList;
public class Notebook {
private ArrayList<String> notes = new ArrayList<String>();
public void add(String s) {
notes.add(s);
}
public void add(String s, int index) {
notes.add(index, s);//指定位置插入
}
public int getSize() {
return notes.size();
}
public String getString(int index) {
return notes.get(index);
}
public void remove(int index) {
notes.remove(index);
}
public String[] list(){
String[] a = new String[notes.size()];
for(int i = 0; i < notes.size(); i++) {
a[i] = notes.get(i);
}
return a;
}
public static void main(String[] args) {
Notebook note = new Notebook();
note.add("坚持");
note.add("胜利");
note.add("逃避", 2);
note.remove(2);
String[] a = note.list();
System.out.println(note.getSize());
System.out.println(note.getString(0));
for(String s: a) {
System.out.println(s);
}
}
}
5.对象数组
6.继承
多态变量
造型cast
大学生属于学生(可以向上造型),但学生不能直接说大学生(类型不匹配)
向上造型
7.多态
函数调用的绑定
覆盖
8.Object类
所有的类都是继承自Object类
Object类的函数
可扩展性vs可维护性
可扩展性:可适应新的东西
可维护性:进行修改后 可适应新的东西
9.消除重复代码
问题引入之城堡游戏
package hello;//Game.java
import java.util.*;
public class Game {
private Room currentRoom;
public Game()
{
creatRooms();
}
private void creatRooms()
{
Room outside, lobby,pub,study,bedroom;
// 制造房间
outside = new Room("城堡外");
lobby = new Room("大堂");
pub = new Room("小酒吧");
study = new Room("书房");
bedroom = new Room("卧室");
// 初始化房间的出口
outside.setExits(null,lobby,study,pub);
lobby.setExits(null,null,null,outside);
pub.setExits(outside,bedroom,null,null);
bedroom.setExits(null,null,null,study);
currentRoom = outside; //从城堡门外开始
}
private void printWelcome()
{
System.out.println();
System.out.println("欢迎来到城堡!");
System.out.println("这是一个超级无聊的游戏。");
System.out.println("如果需要帮助,请输入'help'");
System.out.println();
System.out.println("现在你在:" + currentRoom);
System.out.println("欢迎来到城堡");
System.out.println("出口有:");
if(currentRoom.northExit !=null)
System.out.print("north ");
if(currentRoom.eastExit !=null)
System.out.print("east ");
if(currentRoom.southExit !=null)
System.out.print("south ");
if(currentRoom.westExit !=null)
System.out.print("west");
System.out.println();
}
// 以下为用户命令
private void printHelp()
{
System.out.println("迷路了吗?你可以做的命令有:go bye help");
System.out.println("如:\tgo east");
}
private void goRoom(String direction)
{
Room nextRoom = null;
if(direction.equals("north")){
nextRoom = currentRoom.northExit;
}
if(direction.equals("east")){
nextRoom = currentRoom.eastExit;
}
if(direction.equals("south")){
nextRoom = currentRoom.southExit;
}
if(direction.equals("west")){
nextRoom = currentRoom.westExit;
}
if(nextRoom == null){
System.out.println("那里没有门!");
}
else{
currentRoom = nextRoom;
System.out.println("你在"+ currentRoom);
System.out.println("出口有:");
if(currentRoom.northExit != null)
System.out.println("norh");
if(currentRoom.eastExit != null)
System.out.println("east");
if(currentRoom.southExit != null)
System.out.println("south");
if(currentRoom.westExit != null)
System.out.println("west");
System.out.println();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Game game = new Game();
game.printWelcome();
while (true){
String line = in.nextLine();
String[] words = line.split(" ");
if ( words[0].equals("help")){
game.printHelp();
}else if ( words[0].equals("go")){
game.goRoom(words[1]);
}else if ( words[0].equals("bye")){
break;
}
}
System.out.println("感谢您的光临。再见!");
in.close();
}
}
package hello;\\Room.java
public class Room {
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
public Room(String description)
{
this.description = description;
}
public void setExits(Room north,Room east,Room south,Room west)
{
if(north != null)
northExit = north;
if(east != null)
eastExit = east;
if(south != null)
southExit = east;
if(west != null)
westExit = west;
}
@Override
public String toString()
{
return description;
}
public void southExit(Object object, Room lobby, Room study, Room pub) {
// TODO Auto-generated method stub
}
}
何为好代码
可维护性高
低耦合高内聚
用封装来降低耦合(解耦
用接口来实现耦合
用容器来实现灵活性
10.抽象
抽象在两种语境下的含义
简单应用之细胞自动机
11.数据与表现分离
网格化
12.接口
interface是一种特殊的class
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通