第一周Java技术学习成果文档
一、关于JAVA的基本语法:
1.个人感觉重要的基本概念
* 对象:对象是类的一个实例,有状态和行为。
* 类:类是一个模板,它描述一类对象的行为和状态。
* 方法:方法就是行为,一个类可以有很多方法。逻辑运算、数据修改以及所有动作都是在方法中完成的。
* 实例变量:每个对象都有独特的实例变量,对象的状态由这些实例变量的值决定。
第一个测试程序
public class mclass {
public static void main(String args[])
{
System.out.println("a test");
}
}
2.JAVA中的变量类型、数组、循环结构等
用实例
public static void printArray(int brr[]) //方法
{
for (int i=0;i<brr.length;i++)
{
System.out.println(brr[i]);
}
}
//字符串操作的类
StringBuffer sb = new StringBuffer(12);
sb.append("hyj");
System.out.println(sb);
sb.insert(3,"hyj");
sb.reverse();
System.out.println(sb);
//数组操作
int[] arr =new int[4]; //声明并分配空间
double[][] crr = new double[5][5]; //二维数组
String[][] srr = new String[2][];
srr[0] = new String[1];srr[1] = new String[1];
srr[0][0]="hello"; srr[1][0]="world";
arr[0]=0; arr[2]=9;
int[] brr = {5,2,3,4,1}; //创建并初始化
Arrays.sort(brr);
for (int element:brr) //for each 循环对字符串
{
System.out.println(element);
}
一个用到JAVA递归的例子
/*有一对兔子,从出生后第3个月起都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月兔子的对数为多少?*/
public class own {
public static int sumOfRabbit(int month) {
if (month == 1 || month == 2) {
return 1;
} else {
return sumOfRabbit(month-1)+sumOfRabbit(month-2);
}
}
public static void main(String args[])
{
Scanner input =new Scanner(System.in); //输入
int m=input.nextInt();
System.out.println(sumOfRabbit(m));
}
}
3.JAVA面向对象
继承
多态
重载
抽象类
枚举
enum book
{
book1,book2,book3;
}
for (book mybook:book.values()) //对枚举元素进行迭代
{
System.out.println(mybook);
}
book[] bk = book.values();
for (book mybook : bk)
{
System.out.println(mybook.ordinal()); //查看索引
}
System.out.println(book.valueOf("book2"));
用Java实现单链表
package mypackage;
import java.util.Scanner;
//java实现单链表
public class link {
public static void main(String args[])
{
//单链表调用实例
int n=9999;
Scanner scanner = new Scanner(System.in);
//应当创建带有空的表头节点的单链表
singleList listRoot = new singleList();
//以-1作为结束标识符,依次输入链表的元素
while (n!=-1)
{
n=scanner.nextInt();
listRoot.addToList(n);
}
listRoot.travelList();
System.out.println(singleList.length); //静态变量,可以通过类名直接访问,也可以通过对象访问
// System.out.println(listRoot.next.value);
System.out.println("请输入需要删除的元素");
n=scanner.nextInt();
listRoot.deleteNode(n);
listRoot.travelList();
}
//定义一个单链表的类
public static class singleList
{
int value;
int Flag=1; //如何对类进行标志变量的设置
static int length=-1; //链表长度的记录
singleList next=null; //内部引用,类似指向下一个的指针,根据类定义的对象
public singleList()
{
this.Flag=-1;
}
public singleList (int value) //构造方法
{
this.value = value;
length++;
this.Flag = length;
}
//通过类内置的方法实现链表的操作
//调用该方法实现添加节点(结合判断语句用递归实现)
public void addToList(int n)
{
if (this.next==null)
{
this.next = new singleList(n);
}
else{
this.next.addToList(n);
}
}
//删除指定节点(表头、表中间、表尾)
public boolean deleteNode(int n)
{
if (this.next.value==n&&this.next.next==null)
{
this.next=null;
length--;
return true;
}else if (this.next.value==n){
this.next=this.next.next;
length--;
return true;
}else {
this.next.deleteNode(n);
}
return false;
}
//遍历链表(迭代实现)
public void travelList()
{
if (this.next!=null)
{
if (this.Flag!=-1) {
System.out.println(this.value);
}this.next.travelList();
}
}
}
}
本周学习java过程发现的注意点:
-
一个JAVA源文件只能有一个public类,作为该文件的接口。如果源文件中没有public类,那么文件也可以正常编译,只是会产生多个.class文件,且如果要运行该文件,需要对数据来源进行选择。
-
类内部的静态,可以直接中类名调用而不用实例化为对象。
-
static修饰的成员或方法等在容器加载的时候就已经加载到内存中,比较占用内存资源,且可能在高并发时出错。
-
静态方法调用的三种方法:1.用类名调用2.实例化后用对象调用3.类内部的直接调用(注意静态方法内部不能调用非静态的方法)