数组
一维数组创建
1.直接指定储值的方式:
int [] a= {1,2,3,4};
2.用关键字new初始化数组:
int [] a=new int[10];
二维数组创建
1.直接指定储值的方式:
int [] [] a={{1,2,3},{4,5,6},{7,8,9}};
2.用关键字new初始化数组:
int [] [] a=new int[10][10];
数组基本方法
1.sort排序
public class Car{
public static void main(String [] args) {
int a[]= {3,2,5,1,4};
Arrays.sort(a);
}
}
2.for-each循环
public class Car{
public static void main(String [] args) {
int a[]= {3,2,5,1,4};
for(int i:a) {
System.out.print(i+" ");
}
}
}
3.数组复制
public class Car{
public static void main(String [] args) {
int a[]= {3,2,5,1,4};
int newa1[]=Arrays.copyOf(a, 4); //复制a中的前四个元素 既3 2 5 1
int newa2[]=Arrays.copyOfRange(a, 0, 4); //复制a中[0,4)元素 既3 2 5 1
}
}
static关键字
目的:static 修饰成员变量或者成员方法 将其类化 以后的访问就不用创建对象 可直接在本类中使用
this关键字
目的: 成员变量与局部变量区分,
用法:this.成员变量
类的定义
现实世界一类事物
属性 :事物的状态信息
行为:事物能干什么
java中的类即class也是如此
成员变量:表示事物的属性
成员方法:表示事物的行为
类的定义格式
public class work {
//成员变量
//成员方法
}
类无法直接使用,需要根据类创建一个对象
步骤1:导包 import 包名称.类名
步骤2:创建 格式 类名称.对象名 = new 类名称();
步骤3:使用
使用成员变量:对象名.成员变量名
使用成员方法:对象名.成员方法名(参数)
举个例子
public class Car{
String brand;
String color;
public void show(){
System.out.println( "汽车的品牌是:" + brand);
System.out.println( "汽车的颜色是:" + color);
}
public static void main(String[] args) {
Car car =new Car(); //以Car类创建一个car对象
car.show(); //引用类中的成员方法
}
}
public class Game {
String name;
String type;
String size;
String introduce;
public void show() {
System.out.println( "游戏的名字是:" + name);
System.out.println( "游戏的类型是:" + type);
System.out.println( "游戏的大小是:" + size);
System.out.println( "游戏的介绍是:" + introduce);
}
public static void main(String[] args) {
Game game =new Game(); //创建一个game对象
game.show(); //引用类中的成员方法
}
}
public class Employee {
String name;
String telephone;
int salary;
int growth;
int new1; //薪水增长额
int new2; //总薪水
public String getname() {
return name;
}
public String gettelephone() {
return telephone;
}
public int getsalary() {
return salary;
}
public void x1(){
new1=salary*growth;
}
public void x2(){
new2=salary*growth+salary;
}
}
public class Book {
private String title;
private int pageNum;
public Book() {
}
/**
* 无参构造
*/
public Book(String title,int pageNum) {
this.title=title;
if(pageNum<200) {
System.out.println("错误信息!");
this.pageNum=200;
}
else
this.pageNum=pageNum;
}
/**
* 有参构造
*/
public String GetTitle() {
return this.title;
}
public void SetTitle(String title) {
this.title=title;
}
public int GetPangNum() {
return this.pageNum;
}
public void SetpageNum(int pageNum) {
this.pageNum=pageNum;
}
public void detail() {
System.out.println("这本书的名称是: "+this.title);
System.out.println("这本书的页数是: "+this.pageNum);
}
}
测试代码
public class BookTest {
public static void main(String [] args) {
Book book1=new Book("斗罗大陆",199);
Book book2=new Book("斗破苍穹",400);
/**
* 有参构造两个book1和book2对象进行测试
*/
book1.detail();
System.out.println("=============");
book2.detail();
}
}
测试结果
==============================
public class People {
private String name;
private int age;
private boolean male;
/**
* 是男的就是true 是女的就是false
*/
private static int room;
private static String subject;
/**
* 这里用static修饰room和subject 说明这两个属性是每个对象的共同属性
*/
public People(){
}
public People(String name,int age,boolean male,int room,String subject){
this.name=name;
this.age=age;
this.male=male;;
this.room=room;
this.subject=subject;
}
public void show(){
System.out.println("姓名是:"+this.name+",年龄是:"+this.age+",是不是男的:"+this.male
+",教室在:"+this.room+",所学课程是:"+this.subject);
}
public static void main(String [] args) {
People people1=new People();
people1.name="吕泽浩业";
people1.age=20;
people1.male=true;
people1.room=529;
people1.subject="java";
People people2=new People();
people1.show();
System.out.println("=============");
people1.name="董嘉兴";
people1.age=21;
people1.male=true;
people1.show();
/**
* 这里没有对董嘉兴的教室和课程赋值,但是结果却显示为529和java 说明这两个属性是对象共有的属性
*/
}
}
打印结果
public class Shirt {
private String color;
private int id;
private static int idCount=0;
/**
* idCount是计数器,每增加一个对象,它对应的序号就依次增加
* idCount用关键字static修饰,表明它是所有对象共有属性
*/
public Shirt() {
this.id=++idCount;
}
public Shirt(String color) {
this.id=++idCount;
this.color=color;
}
public void print() {
System.out.println(this.id);
}
public static void main (String [] args) {
Shirt shirt1=new Shirt("黑红色");
/**
* 有参构造
*/
shirt1.print();
System.out.println("=============");
Shirt shirt2=new Shirt();
shirt2.print();
/**
* 无参构造
*/
}
}
运行结果