概要
public class Demo2 {
String type; //品种
double weight; //重量
double height; //身高
String species; //种属
String hobby; //爱好
public void eat(){
System.out.println("给我吃");
}
public void run(){
System.out.println("给我跑");
}
public String cry(){
return "狗叫什么";
}
}
public class TestDemo2 {
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
demo2.type = "中华田园犬";
demo2.weight = 50.50;
demo2.height = 77.77;
demo2.species = "宠物";
demo2.hobby = "接飞盘";
String cry = demo2.cry();
demo2.eat();
demo2.run();
System.out.println(cry);
}
}
public class TestDemo3 {
public static void main(String[] args) {
Demo3 demo3 = new Demo3();
demo3.sex = "人妖";
demo3.age = 100;
demo3.height = 150.98;
demo3.weight = 150.98;
demo3.hoppy = "玩花活";
System.out.println("性别是:" + demo3.sex);
System.out.println("年龄是:" + demo3.age);
System.out.println("身高是:" + demo3.height);
System.out.println("体重是:" + demo3.weight);
System.out.println("爱好是:" + demo3.hoppy);
String cry = demo3.cry();
demo3.eat();
demo3.run();
System.out.println(cry);
}
}
public class Demo3 {
String sex;
int age;
double height;
double weight;
String hoppy;
public void eat(){
System.out.println("给我吃");
}
public void run(){
System.out.println("给我跑");
}
public String cry(){
return "叫什么";
}
}
public class User {
Scanner sc = new Scanner(System.in);
String username = "admin";
String password = "123456";
String Newusername;
String Newpassword;
public void amend(){
System.out.println("******密码修改******");
System.out.print("输入旧的用户名:");
Newusername = sc.next();
System.out.print("输入旧的密码:");
Newpassword = sc.next();
System.out.println("******验证成功******");
while (!(Newusername.equals(username)) || !(Newpassword.equals(password))){
System.out.println("您输入的用户名或者密码有误!");
System.out.print("输入旧的用户名:");
Newusername = sc.next();
System.out.print("输入旧的密码:");
Newpassword = sc.next();
}
System.out.print("请输入新的密码:");
Newpassword = sc.next();
System.out.println("******修改成功******");
}
}
//运行
public class TestUser {
public static void main(String[] args) {
User user = new User();
user.amend();
}
}
封装
封装代码操作
需求说明:
创建轿车类,类中包含汽车的品牌、型号售价、百公里油耗封装汽车的各个属性
得出预计结果后尝试优化和调整代码编写方式
public class TestCar {
public static void main(String[] args) {
Car car = new Car();
car.setBrand("梅赛德斯");
car.setModelnumber("大G4×4");
car.setPrice(699.99);
car.setFuel(60.6);
String brand = car.getBrand();
String modelnumber = car.getModelnumber();
double price = car.getPrice();
double fuel = car.getFuel();
System.out.println("品牌是:" + brand);
System.out.println("型号是:" + modelnumber);
System.out.println("售价是:" + price + "w起");
System.out.println("百公里油耗是" + fuel + "L");
}
}
public class Car {
private String brand;
private String modelnumber;
private double price;
private double fuel;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModelnumber() {
return modelnumber;
}
public void setModelnumber(String modelnumber) {
this.modelnumber = modelnumber;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getFuel() {
return fuel;
}
public void setFuel(double fuel) {
this.fuel = fuel;
}
}
需求说明:
创建卡车类,类中包含汽车的品牌、型号售价、最大载重封装汽车的各个属性得出预计结果后尝试优化和调整代码编写方式
public class TestTruck {
public static void main(String[] args) {
Truck truck = new Truck();
truck.setBrand("解放");
truck.setModelnumber("J7-650马力");
truck.setPrice(80.77);
truck.setMaximumloadbearing(60.6);
String brand = truck.getBrand();
String modelnumber = truck.getModelnumber();
double price = truck.getPrice();
double maximumloadbearing = truck.getMaximumloadbearing();
System.out.println("品牌是:" + brand);
System.out.println("型号是:" + modelnumber);
System.out.println("售价是:" + price + "w起");
System.out.println("最大承重:" + maximumloadbearing + "吨");
}
}
public class Truck {
private String brand;
private String modelnumber;
private double price;
private double maximumloadbearing;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModelnumber() {
return modelnumber;
}
public void setModelnumber(String modelnumber) {
this.modelnumber = modelnumber;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getMaximumloadbearing() {
return maximumloadbearing;
}
public void setMaximumloadbearing(double maximumloadbearing) {
this.maximumloadbearing = maximumloadbearing;
}
}
小结
提供先进的推理,复杂的指令,更多的创造力。