本文作为【实训笔记】第三天:数组和面向对象(1)的代码补充。
目录
1、数组
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("-----");
}
}
public class ArrayDemo02 {
public static void main(String[] args) {
//声明数组
int[] arr = new int[20];
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
// System.out.println(arr[5]); ArrayIndexOutOfBoundsException
//数组的遍历
for (int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}
2、面向对象
类、对象、引用的基本概念
/**
* @author zn
* 编程实现Person类的定义,特征有:姓名和年龄
* 要求在main方法声明Person类型的引用指向Person类型的对象并打印特征,
* 再将姓名和年龄修改为"zhangfei"和30后再次打印
*/
public class Person {
String name;
int age;
/**既不需要向方法体外传递数据内容,也不需要向方法体内输入数据内容*/
public void show(){
System.out.println("姓名: " + name);
System.out.println("年龄: " + age);
}
public static void main(String[] args) {
Person p = new Person();
p.show();
p.name = "zhangfei";
p.age = 30;
p.show();
}
}
/**
* @author asus
* 自定义Phone类,特征有:品牌和价格,行为有:打印品牌和价格的方法
* 要求在main()方法声明Phone类型的引用指向Phone类型的对象并打印特征
* 将品牌和价格修改为"Nokia"和598.5,并再次打印
*/
public class Phone {
String brand;
double price;
/*当用户没有写任何形式的构造方法时,系统会自动的为程序提供一个无参构造方法*/
/*
public Phone(){
System.out.println("我是无参构造,我正在被调用");
}
*/
/**如果用户自己编写了构造方法(有参或无参构造方法,系统将不会在为我们提供任何形式的构造方法)*/
// public Phone(String name){
// System.out.println("有参构造方法");
// }
public void show(){
System.out.println("品牌: " + brand + ",价格: " + price);
}
/**是谁在调用main方法:JVM Java虚拟机*/
public static void main(String[] args) {
Phone p = new Phone();
p.show();
p.brand = "Nokia";
p.price = 598.5;
p.show();
}
}
成员方法(行为)
public class Demo01 {
public static void main(String[] args) {
int a = 1;
String name = "张三";
char c = 'a';
boolean b = true;
System.out.println(a);
System.out.println(name);
System.out.println(c);
System.out.println(b);
}
}
/**
* @author zn
* 编程实现Girl类的定义,特征有:姓名、年龄、是否有用男朋友bf,行为有:打印所有特征的方法,
* 要求在main方法中声明Girl类型的引用指向Girl类型的对象并打印特征,
* 然后修改特征为"貂蝉",18以及true后再次打印
*/
public class Girl {
String name;
int age;
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();
}
}
构造方法和方法重载
/**
* @author asus
* 实体类 bean/pojo/domain/entity
*/
public class Person2 {
private String name;
private int age;
public Person2() {
}
public Person2(String name, int age) {
this.name = name;
this.age = age;
}
public void show(){
setAge(getAge());
System.out.println("name = " + name + ",age = " + age);
}
//单元测试
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0 && age < 150){
this.age = age;
}else {
this.age = 18;
System.out.println("您输入的年龄有误");
}
}
}
/**
* @author asus
* 定义一个坐标类,特征有,横坐标x和纵坐标y
* 创建坐标类的对象,讲对像的信息打印出来,修改特征信息,并再次打印。
*/
public class Point {
int x;
int 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);
}
}
public class Circle {
int r;
double PI = 3.14;
public Circle() {
this.r = 4;
}
public double area(){
return PI * r * r;
}
public double perimeter(){
return 2 * PI * r;
}
public static void main(String[] args) {
Circle c = new Circle();
c.area();
c.perimeter();
}
}
public class Vehicle {
double s;
String type;
public Vehicle(double s, String type) {
this.s = s;
this.type = type;
}
public double getSpeed() {
return s;
}
public void setSpeed(double s) {
this.s = s;
}
public void speedup(double s) {
this.s = s + 10;
}
public void speedDown(double s) {
this.s = s - 10;
}
@Override
public String toString() {
return "Vehicle{" +
"s=" + s +
", type='" + type + '\'' +
'}';
}
public static void main(String[] args) {
Vehicle v = new Vehicle(50, "小汽车");
System.out.println(v.toString());
v.speedup(v.getSpeed());
System.out.println(v.toString());
v.speedDown(v.getSpeed());
v.speedDown(v.getSpeed());
System.out.println(v.toString());
}
}
public class Rectangle {
int width;
int length;
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public int getWidth() {
return width;
}
public int getLength() {
return length;
}
public int getArea(){
return getWidth() * getLength();
}
public int getPer(){
return 2 * (getWidth() + getLength());
}
public String showAll(){
return "长:" + length + " 宽:" + width + " 面积:" + getArea() + " 周长:" + getPer();
}
public static void main(String[] args) {
Rectangle r = new Rectangle(3, 4);
System.out.println(r.showAll());
}
}
public class Car {
String brand;
String color;
double price;
public Car() {
}
public Car(String brand, String color, double 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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void priceUp(){
this.price = getPrice() + 1000;
}
public void priceUp(double up){
this.price = getPrice() + up;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", price=" + price +
'}';
}
public static void main(String[] args) {
Car car = new Car();
System.out.println(car.toString());
Car car1 = new Car("劳斯莱斯", "红", 5000000);
System.out.println(car1.toString());
car1.priceUp();
System.out.println(car1.toString());
car1.priceUp(1000000);
System.out.println(car1.toString());
}
}