1.数组
一维数组:声明数组的本质就是在内存中申请一段连续的存储单元。
格式:数据类型【】数组名 = new 数据类型【数组的长度】
数组的初始化:在数组声明时,如果不给数组直接赋值,那么byte/short/int/long/的默认值为0,float和double的默认值为0.0,布尔类型的默认值是false,char的默认值0或者\u000。
2.面向对象:
面向对象的本质就是分析事物的特征(名词)和行为(动词)
面向对象编程是先用面向对象的思想进行分析,再用面向对象的语言进行翻译的过程。
对象是指客观存在的实体
语法格式:new 引用数据类型();
类:具有相同特征和行为的多个事物共性的抽象,在Java中表现为一种引用数据类型,其中包含描述特征的成员变量和描述行为的成员方法;
通常情况下,类名的首字母要大写,当类名由多个单词构成时,每个单词的首字母都要大写;(大驼峰命名法)
通常情况下,成员变量名的首字母要小写,当成员变量名由多个单词构成时,从第二个单词开始,首字母要大写;(小驼峰命名法)
引用:引用类型的成员变量名
***类是对象的抽象,对象是类的实现
3.构造方法和方法重载
构造方法:无参,有参,全参的构造方法
注意:构造方法名与类名完全相同并且没有返回值类型,连void都不许有;当使用new关键字的创建对象的时会自动调用构造方法来实现成员变量的工作;
默认构造方法:当一个类中没有定义任何构造方法时,编译器会自动添加一个无参空构造方法,叫做默认/缺省构造方法,如public Person() {};若类中出现了构造方法,则编译器不再提供任何形式的构造方法。
方法重载:在Java语言中若方法的名称相同,但参数列表不同,这样的方法之间构成重载关系(overload)
4.面向对象的三大特征
this关键字:
基本概念:在构造方法中和成员方法中访问成员变量时,编译器会加上this.的前缀 。当不同的对象调用同一个方法时,由于调用方法的对象不同导致this关键字不同,从而this.方式访问的结果也就随之不同。
封装:
实体类封装的步骤:
1、私有化成员变量;
2、提供公有的get/set方法
3、提供无参/有参/全参的构造方法
4、重写toString()、equals和hashCode()
二、代码以及运行截图
package ThreeDay; public class ArrayDemo01 { public static void main(String[] args) { //声明数组 int[] arr = new int[5]; System.out.println(arr[0]); boolean[] brr = new boolean[5]; System.out.println(brr[0]); char[] crr = new char[5]; System.out.println(crr[0]); if(crr[0] == 0) { System.out.println("char数组的默认值为0"); } System.out.println("-----------"); } }
package ThreeDay; /** * @author zcl */ public class ArrayDemo02 { public static void main(String[] args) { //声明数组 int[] arr = new int[5]; //数组的遍历 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
package ThreeDay; import java.util.Random; //双色球练习数组 public class DoubleColorBall { public static void main(String[] args) { //声明一个int类型 长度为7的一维数组,动态 int[] arr = new int[7]; //创建一个随机数对象 Random random = new Random(); //开始摇号(向数组当中去添加值),需要先摇6红球范围是1到33 for (int i = 0; i < arr.length - 1; i++) { //红球 arr[i] = random.nextInt(33) + 1; //去重 for (int j = i - 1; j >= 0; j--) { //表示号码重复了 if (arr[i] == arr[j]) { //若号码出现重复,需要重新摇号 i--; break; } } } //蓝球 范围是1到17 arr[arr.length - 1] = random.nextInt(17) + 1; //将数组中的双色球遍历 System.out.println("本期中奖结果是:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
package ThreeDay; public class Girl { private String name; private int age; private boolean bf; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isBf() { return bf; } public void setBf(boolean bf) { this.bf = bf; } public void show() { System.out.println("姓名:" + name + ",年龄:" + age + ",是否有男朋友?" + bf); } public static void main(String[] args) { Girl girl = new Girl(); girl.show(); girl.name = "貂蝉"; girl.age = 18; girl.bf = true; girl.show(); girl.setAge(20); girl.setName("赵纯俪"); girl.setBf(false); girl.show(); } }
package ThreeDay; public class MethodDemo01 { public int add(int a, int b) { return a + b; } public static void main(String[] args) { MethodDemo01 m = new MethodDemo01(); int sum = m.add(1,2); System.out.println("sum = " + sum); } }
package ThreeDay; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void show() { System.out.println("姓名:" + name + ",年龄:" + age); } public static void main(String[] args) { Person person = new Person(); person.show(); person.name = "zhangfei"; person.age = 30; person.show(); } }
package ThreeDay; import org.junit.Test; public class PersonTest { @Test public void test01(){ Person p = new Person(); p.show(); p.getName(); } @Test public void test02(){ Phone phone = new Phone(); phone.show(); } }
package ThreeDay; public class Phone { private String brand; private double price; //构造方法用来初始化对象 //当用户没有写任何形式的构造方法时,系统会自动的为程序提供一个无参构造方法 //无参 既不需要向方法体外传递数据内容,也不需要向方法体内传递数据内容 public Phone(){ System.out.println("无参构造方法。。。"); } //如果用户自己编写了构造方法(有参或无参构造方法,系统将不会再为我们提供任何形式的构造方法) public Phone(String name) { System.out.println("有参构造方法。。。"); } public void show() { System.out.println("品牌:" + brand + ",价格:" + price); } //JVM java虚拟机调用main方法 public static void main(String[] args) { Phone phone = new Phone(); phone.show(); phone.brand = "NoKia"; phone.price = 598.5; phone.show(); } }
package ThreeDay; public class Point { int x,y; public static void main(String[] args) { //创建一个Point类型的引用point,指向Point类型的对象 Point point = new Point(); System.out.println("x = " + point.x); System.out.println("y = " + point.y); point.x = 3; point.y = 4; System.out.println("x = " + point.x); System.out.println("y = " + point.y); } }
package Practice03; public class Car { private String brand; private String color; private int price; public Car() {}; public Car(String brand, String color, int price) { this.brand = brand; this.color = color; this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public void show() { System.out.println("该" + brand + "车的颜色是" + color + ",价格是:" + price); } public int increase() { price = price + 1000; return price; } public int increase(int a) { price = price + a; return price; } public static void main(String[] args) { Car c = new Car(); c.brand = "宝马"; c.color = "绿色"; c.price = 500000; c.show(); Car c1 = new Car("奥迪", "黑色", 300000); c1.show(); c1.increase(); c1.increase(5200); c1.show(); } }
package Practice03; public class Circle { private int r; public double Area() { return Math.PI * r * r; } public double Girth() { return 2 * Math.PI * r; } public Circle(int r) { this.r = r; } public static void main(String[] args) { Circle circle = new Circle(4); circle.Area(); circle.Girth(); System.out.println("圆的面积为:" + circle.Area() + ",圆的周长为:" + circle.Girth()); } }
package Practice03; public class Point { private int x; private int y; public Point() {} public Point(int x, int y) { this.x = x; this.y = y; } public void show() { System.out.println("x:" + x + ",y:" + y); } public int addX() { x = x + 1; return x; } public int addY(int a) { y = y + a; return y; } public static void main(String[] args) { Point p = new Point(); Point p1 = new Point(1, 2); p.show(); p1.show(); p1.addX(); p1.addY(2); p1.show(); } }
package Practice03; public class Rectangle { private int length; private int width; public Rectangle() {}; public Rectangle(int length, int width) { this.length = length; this.width = width; } public int getArea() { return width * length; } public int getPer() { return 2 * (width + length); } public void showAll() { System.out.println("该矩形的长是:" + length + ",宽是:" + width + ",面积是:" + getArea() + ",周长是:" + getPer()); } public static void main(String[] args) { Rectangle rectangle = new Rectangle(3, 4); rectangle.getArea(); rectangle.getPer(); rectangle.showAll(); } }
package Practice03; public class Vehicle { private double speed; private String type; public Vehicle() {} public Vehicle(double speed, String type) { this.speed = speed; this.type = type; } public void move() { System.out.println(type + "车正在以" + speed + "的速度前行。"); } public double setSpeed(double s) { speed = s; return speed; } public double speedUp(double s) { speed = speed + s; return speed; } public double speedDown(double s) { speed = speed - s; return speed; } public static void main(String[] args) { Vehicle vehicle = new Vehicle(35.68, "宝马"); vehicle.move(); vehicle.speedDown(5); vehicle.move(); vehicle.speedUp(10); vehicle.move(); vehicle.setSpeed(25); vehicle.move(); } }