java基础知识第十八天

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

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一...
    阿里高级软件架构师阅读 3,314评论 0 19
  • 废话不多说,自己进入今天的主题 1、面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:...
    传奇内服号阅读 2,402评论 1 31
  • 在经历接二连三的感冒与从未终止的口腔疡后我终于下决心每天早上跑步。 从8月开始,到今天有两个多月了。能坚持下来得归...
    诺伊蝶阅读 1,161评论 11 16
  • 记得上小学时,老师给我们放了一部动画片《千与千寻》,然后我就看到了至今为止很爱的动画片。后来也看了宫崎骏的很多动画...
    隐士喵阅读 2,136评论 32 26
  • 已经放假了,我走着的这条老街在搞规整,旧的坑坑洼洼还没来得及填上,新的倒是又挖了不少,这样的路没几个...
    玙珏阅读 1,051评论 3 4