package demo0124;
public class demo01 {
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("-------");
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/ebb4a3d14a8f4faa9e86945bc0682ef4.png)
package demo0124;
public class demo02 {
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]);*/
//数组的遍历
// 为数组元素赋值
/*for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1; // 或者其他你想要的值
}遍历出值*/
for (int i=0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/eeb4d28183777c4b37221c5a1867f7f3.png)
package demo0124;
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;
}
}
//蓝球
arr[arr.length - 1] = random.nextInt(17)+1;
//将数组中的双色球遍历
System.out.println("本期中奖结果:");
for (int i = 0;i< arr.length;i++){
System.out.println(arr[i]+" ");
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/5da44a214dc2602282c5168d61f15886.png)
package demo0124;
public class Girl {
String name;
int age;
boolean bf;
public void print() {
System.out.println("姓名: " + name + ", 年龄: " + age + ", 是否有男朋友: " + 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 static void main(String[] args) {
demo0124.Girldemo girl = new demo0124.Girldemo();
girl.print();
girl.name = "貂蝉";
girl.age = 18;
girl.bf = true;
girl.print();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6ded4b0168b0da0f168e87e65544b94f.png)
package demo0124;
public class Girldemo {
String name;
int age;
boolean bf;
public void print() {
System.out.println("姓名: " + name + ", 年龄: " + age + ", 是否有男朋友: " + bf);
}
public static void main(String[] args) {
Girldemo girl = new Girldemo();
girl.print();
girl.name = "貂蝉";
girl.age = 18;
girl.bf = true;
girl.print();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/9a5fe789ceca54448e90d45668ef8478.png)
package demo0124;
public class MethodDemo {
public int add(int a,int b){
return a+b;
}
public static void main(String[] args) {
MethodDemo m = new MethodDemo();
int sum = m.add(1,2);
System.out.println("sum=" + sum);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/e4b4109c49e961c3176518f9dd80ba2e.png)
package demo0124;
public class Person {
String name;
int age;
// Person类的构造函数
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 打印Person对象的特征
public void show() {
System.out.println("Name: " + this.name + ", Age: " + this.age);
}
public static void main(String[] args) {
// 声明Person类型的引用指向Person类型的对象
Person person = new Person("lilei", 20);
// 打印Person对象的特征
person.show();
// 修改Person对象的姓名和年龄
person.name = "zhangfei";
person.age = 30;
// 再次打印Person对象的特征
person.show();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/341c55cea73d79ec583b4af9b67d5d61.png)
package demo0124;
public class Phone {
String brand;
double price;
public void show(){
System.out.println("品牌:" + brand +",价格:" + price);
}
public static void main(String[] args){
Phone p =new Phone();
p.show();
p.brand = "Nokia";
p.price = 598.5;
p.show();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/750909e21dcd390512701b03dca2fba9.png)
package demo0124;
public class Phone01 {
//当用户没有写任何形式的构造方法时,系统会自动地为程序提供一个午餐构造方法
String brand;
double price;
/*public Phone01(){
System.out.println("我是无参构造.....正在被调用");
}*/
//如果用户自己编写了构造方法(有参或无参构造方法,系统将不会再为我们提供任何形式的构造方法)
public Phone01(String name){
System.out.println("有参构造方法...");
}
public Phone01(){}
//既不需要向方法体外传递数据内容,也不需要向方法体内传递数据内容
public void show(){
System.out.println("品牌:" + brand +",价格:" + price);
}
public static void main(String[] args){
Phone01 p = new Phone01();
p.show();
p.brand = "Nokia";
p.price = 598.5;
p.show();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/887c863b3fd8af8917a9d99825779e2f.png)
package demo0124;
public class Point {
int x;
int y;
public static void main(String[] args){
//创建一个Piont类型的引用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);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/c8563e99b695096f43bdd425c7d12b5f.png)
package demo0124;
public class Point01
{
int x;
int y;
void down() {
x++;
}
void down(int dy){
y+=dy;
}
void show(){
System.out.println("横坐标"+x+"纵坐标"+y);
}
public static void main(String[] args){
Point01 p = new Point01();
p.x=1;
p.y=2;
p.show();
p.down();
p.show();
p.down(3);
p.show();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/e092b8c788ec45b24dcffb35b55d712b.png)
package demo0124;
public class Vehicle {
int speed, type;
void move() {
}
void setSpeed(int speed) {
this.speed = speed;
}
int getSpeed() {
return speed;
}
void setType(int Type) {
this.type = type;
}
int getType(){
return type;
}
void speedUp()
{
this.speed++;
}
void speedDown()
{
this.speed--;
}
public static void main(String args[]) {
Vehicle v1=new Vehicle();
v1.setType(3);
v1.setSpeed(5);
System.out.println("类型为:"+v1.getType());
System.out.println("速度为:"+v1.getSpeed());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/cd529a1c061743f4a067e6e53bb0d820.png)
package demo0124;
public class Circle {
float r;
final double PI = 3.14;
public double s() {
return PI * r * r;
}
public double c() {
return 2 * PI * r;
}
public Circle() {
r = 4;
}
public static void main(String[] args) {
Circle a = new Circle();
System.out.println("圆面积:" + a.s());
System.out.println("圆周长:" + a.c());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/26fa3a03e3bf4e63413c3634a5ea9db8.png)
package demo0124;
public class Rectangle {
int length;
int width;
//构造方法
Rectangle(int length,int width){
this.length=length;
this.width=width;
}
//控制台显示长,宽,面积,周长
public void showAll(){
System.out.println("矩形长为:"+length+"\t"+"矩形的宽为:"+width+"\t"+"矩形的面积为:"+getArea()+"\t"+"矩形的周长为:"+getPer());
}
//求周长
public int getPer(){
return 2*(length+width);
}
//求面积
public int getArea(){
return length*width;
}
//
}
class RectangleDemo{
public static void main(String[] args){
Rectangle r=new Rectangle(2,3);
r.showAll();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/2caae202759b1310c90c910a14ceb8c5.png)
package demo0124;
public class Car {
private String brand;
private String color;
private int price;
// 无参构造函数
public Car() {
this.brand = "";
this.color = "";
this.price = 0;
}
// 有参构造函数
public Car(String brand, String color, int price) {
this.brand = brand;
this.color = color;
this.price = price;
}
// 打印所有特征
public void printCar() {
System.out.println("品牌: " + brand);
System.out.println("颜色: " + color);
System.out.println("价格: " + price);
}
// 获取品牌并返回
public String getBrand() {
return brand;
}
// 获取颜色并返回
public String getColor() {
return color;
}
// 获取价格并返回
public int getPrice() {
return price;
}
// 设置品牌为参数指定的数值
public void setBrand(String brand) {
this.brand = brand;
}
// 设置颜色为参数指定的数值
public void setColor(String color) {
this.color = color;
}
// 设置价格为参数指定的数值,并使价格增长1000元
public void setPrice(int price) {
this.price = price + 1000;
}
// 设置价格增长参数指定数值的行为
public void setPriceIncrease(int increment) {
this.price += increment;
}
public static void main(String[] args) {
// 使用无参形式构造对象并打印特征
Car car1 = new Car();
car1.printCar(); // 输出:品牌: 颜色: 价格: 0
// 使用有参形式构造对象并打印特征
Car car2 = new Car("Toyota", "Red", 10000);
car2.printCar(); // 输出:品牌: Toyota 颜色: Red 价格: 10000
// 使用有参形式构造的对象调用其他方法测试
System.out.println("car2的品牌: " + car2.getBrand()); // 输出:car2的品牌: Toyota
System.out.println("car2的颜色: " + car2.getColor()); // 输出:car2的颜色: Red
System.out.println("car2的价格: " + car2.getPrice()); // 输出:car2的价格: 10000
car2.setBrand("Ford");
car2.setPriceIncrease(300);
System.out.println("car2的价格增长了300元后变为: " + car2.getPrice());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/28f693970be160b2cef44dac72f5e11a.png)