1.做一个汽车类的练习
//-----------汽车类-------------------
public class Car {
String brand;
private int price;
public void setCar(int price){
if(price<=0){ //对传入的参数进行检查
System.out.println("你输入的数字不合法!");
}else{
this.price=price; //对属性赋值
}
}
public int getCar(){
return price;
}
public void cars(){
System.out.println("类封装性的应用");
System.out.println(brand+"======"+price);
}
public void car(){
System.out.println("无参构造方法");
}
public void car(String brand,int price){
System.out.println("有参的构造方法");
this.brand=brand;
this.price=price;
System.out.println("汽车的品牌是:"+this.brand);
System.out.println("汽车的价格是:"+this.price);
}
}
//----------------汽车类的测试类----------------------
public class CarTest {
public static void main(String[] args) {
Car c=new Car();
c.brand="大众";
c.setCar(-80000);//你输入的数字不合法!
System.out.println("===================");
c.cars(); //大众======0
System.out.println("===================");
c.car(); //无参构造方法
System.out.println("===================");
c.car("奥迪",1000000); //有参的构造方法
// 汽车的品牌是:奥迪
//汽车的价格是:1000000
}
}
****汽车类的运行结果******
2.String类的判断功能
列举:a.boolean equals(Object obj) //比较字符串内容是否相等
b.boolean equalsIgnoreCase(String str) //比较字符串内容是否相同,忽略大小写
c.boolean startsWith(String str) //比较字符串内容是否以指定的str开头
d. boolean endsWith(String str) //比较字符串内容是否以指定的str 结尾
public class StringDemo {
public static void main(String[] args) {
String s1="hello";
String s2="hello";
String s3="Hello";
// boolean equals(Object obj) //比较字符串内容是否相等
System.out.println(s1. equals (s2));//true
System.out.println(s1. equals (s3));//false
//boolean equalsIgnoreCase(String str) //比较字符串内容是否相同,忽略大小写
System.out.println(s1. equalsIgnoreCase (s2));//true
System.out.println(s1. equalsIgnoreCase (s3));//true
//boolean startsWith(String str) //比较字符串内容是否以指定的str开头
System.out.println(s1. startsWith ("he"));//true
System.out.println(s1. startsWith ("ll"));//false
System.out.println("--------------------");
//boolean endsWith(String str) //比较字符串对象是否以指定str结尾
System.out.println(s1.endsWith("lo"));//true
System.out.println(s1.endsWith("he"));//false
}
}