java基础3

来源:互联网 发布:d3.js 实战手册 pdf 编辑:程序博客网 时间:2024/06/02 02:13

package com.edu_01;

public classStudent {

    //成员变量

    Stringname;

    int age;

    //成员方法

    public void makePlane(){

        System.out.println("学生会造飞机");

    }  

    public void eat(){

        System.out.println("饿了就上饿了吗");

    }

    //提供一个检测年龄的方法

    public void checkAge(int a){

        if (a>100||a<0) {

            System.out.println("您填写的年龄不符合规则");

        }else {

            age = a;

        }

    }

}

 

package com.edu_01;

public classTest {

    public static void main(String[] args) {

        //创建学生对象

        Students = newStudent(); 

        //给学生对象的姓名和年龄赋值

        s.name = "华仔";

        //s.age = -10;

        //使用我们刚刚创建的检测年龄的方法给学生年龄赋值

        s.checkAge(-20);

        s.age = -10;   

        //打印学生的成员变量

        System.out.println(s.name+"  "+s.age);

    }

}

package com.edu_02;

/**

 * 1.2

    封装加入private后的标准代码:

    A:把成员变量private修饰

    B:针对每一个成员变量给出

        getXxx()和setXxx()(注意首字母大写)

        注意:这里的xxx其实是成员变量名称。

 *

 */

/**

二:this关键字详解(演示修改局部变量的变量名和成员变量相同的话的效果)

    常说:

        见名知意。

    局部变量如果有一个变量和成员变量名称一致,那么优先使用的是局部变量。

    就近原则。

    这样的话,就造成了局部变量隐藏了成员变量。s

    如何解决呢?

        为了解决这种情况,java就提供了一个代表本类对象的关键字:this

        this这个关键字指代变本类中的一个对象,但是具体指代本类中的哪个对象呢?

        -- 谁调用我我指代谁

 *

 */

 

public class Student { 

    privateString name;

    privateint age;//私有化成员变量,被私有化的成员变量只能在本类中进行访问   

    /**

     * 我们会针对每一个私有化的成员变量对外提供一个公共的访问方式:

     * 例如私有化的成员变量name就需要对他提供设置和获取的两个公共的访问方式,

     * setName(String name),getName();

     * 依据以上的举例:

     * 我们必须针对每一个私有化的成员变量提供setXxx(),getXxx()

     * 的方法让外界通过这两个方法访问我们的私有化成员

     */

    //为name这个成员变量提供访问方式

    publicvoid setName(String name){

        this.name= name;//s.name = "黎明";

        /**

         * java中的变量访问遵循就近原则:

         * 如果我们将局部变量和成员变量名称改为一致的话,就无法访问到成员变量了

         * ,此时的赋值仅仅是给我方法中的局部变量进行赋值,并没有给我的成员变量赋值,

         * 因为此刻我根本无法访问到我的成员变量

         */

    }

   

    publicString getName(){

        returnname;

    }  

    //为age提供set/get方法

    publicvoid setAge(int age){

        this.age= age;//s.age = 40;

    }

    publicint getAge(){

        returnage;

    }  

   

    publicvoid eat(){

        System.out.println("饿了要吃饭");

    }

 

    public voidsleep(){

        System.out.println("困了需要睡觉");

    }

}

package com.edu_02;

/**

 * 一:

    1.1

    封装:(案例演示,创建学生类年龄可以随意被设置成不符合要求的参数)

        是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。

    好处:

        A:提高了代码的复用性

        B:提高安全性。

    体现:

        将不需要对外提供的内容都隐藏起来。

        做法:

            把属性隐藏,提供公共方法对其访问。

 

 

    现在我们可以给age赋值,但是赋予负数值居然也通过了。这是不满足实际需求的。

    我们应该对不满足需求的数据给与判断。

    而判断不能直接针对成员变量做,因为要判断,就必须写在方法中。

    所以,我们应该用一个方法给年龄赋值并判断。

    虽然我们已经通过一个方法做出了数据不正常的判断,但是我们还是可以通过对象直接访问成员变量。

    那么,为了让使用者使用方法来给成员变量赋值,就不能允许它们直接去访问成员变量。

    怎么办呢?

        为了解决这种情况,java就提供了一个修饰符关键字:private

    private

        私有的意思。

        可以修饰成员变量和成员方法。

        特点:

            private修饰的内容,只能在本类中访问。

 *

 */

public classTest {

    public static void main(String[] args) {

        Students = newStudent();

        //给学生对象的成变量赋值

        //使用set/get方法给学生对象进行赋值

        s.setName("黎明");

        s.setAge(40);

        //get方法进行获取

        System.out.println(s.getName()+"  "+s.getAge());

       

        System.out.println("------------------------");

        Students2 = newStudent();

        s2.setAge(30);

        s2.setName("马云");

        System.out.println(s2.getAge()+"  "+ s2.getName());

    }

}

package com.edu_03;

public classStudent {

    private Stringname;

    private int age;

    public void setName(String name){

        this.name = name;

    }

    public String getName(){

        returnname;

    }

    public void setAge(int age){

        this.age = age;

    }

    public int getAge(){

        returnage;

    }

   

    //写一个无参数的构造方法

    public Student(){

        System.out.println("这是一个无参数的构造方法");

    }

    //写一个有参数的构造方法

    public Student(String name,int age){

        System.out.println("两个参数的构造");

        //在构造方法中给成员变量进行赋值

        this.name = name;

        this.age = age;

    }

}

package com.edu_03;

/**

 * 三:构造方法

    3.1

    构造方法:

        作用:给对象的数据进行初始化

        格式特点:

            A:方法名和类名相同。

                publicvoid Student() {}

            B:没有返回值类型。

                修饰符返回值类型方法名(...) {...}

            C:没有返回值。

                没有用return带明确的值回来。               return;

   

    3.2

    构造方法注意事项

        A:如果你不提供构造方法,系统会给出默认无参构造方法

        B:如果你提供了构造方法,系统将不再提供默认无参构造方法

            这个时候怎么办呢?

                a:使用自己给的带参构造。

                b:要么自己再提供一个无参构造

                建议:建议永远自己给出无参构造方法。

        C:构造方法也是可以重载的。

   

    3.3

    给成员变量赋值:

        A:通过setXxx()方法

        B:通过带参构造方法

 *

 */

public classTest {

    public static void main(String[] args) {

        //创建一个学生对象

        //Student s = new Student();//此时调用的是一个类的无参数的构造方法

        //当你没有提供任何的构造方法的时候,系统会给你提供一个默认的无参数的构造方法

       

        System.out.println("---------------------------");

        //调用Student类中的两个两个参数的构造方法

//      Students = new Student("刘德华", 40);

//      System.out.println(s.getAge()+"  "+s.getName());

       

        System.out.println("----------------------------");

        Students = newStudent();

       

    }

}

package com.edu_04;

/**

 * 五:

标准代码的写法:

练习:

    手机类:

        成员变量:

            brand,price,color

        构造方法:

            无参,带参

        成员方法:

            getXxx()/setXxx()

            show(),call()

           

        一个标准的java描述类:

        1.私有化成员变量

        2.为私有化的成员变量提供set/get方法

        3.提供有参数和无参数的构造方法

        4.还需要写一个功能性的方法

 *

 */

public classPhone {

    //提供私有化的成员变量

    private Stringbrand;

    private int price;

    private Stringcolor;

   

    //为私有化的成员变量提供set/get方法

    public void setBrand(String brand){

        this.brand = brand;

    }

    public String getBrand(){

        returnbrand;

    }

 

    public void setPrice(int price){

        this.price = price;

    }

    public int getPrice(){

        returnprice;

    }

   

    public void setColor(String color){

        this.color = color;

    }

    public String getColor(){

        returncolor;

    }

   

    //提供有参数和无参数的构造方法

    public Phone(){}

   

    public Phone(String brand,int price,String color){

        this.brand = brand;

        this.price = price;

        this.color = color;

    }

   

   

    //提供功能性的方法

    public void call(){

        System.out.println("手机可以打电话");

    }

    public void show(){

        //show方法打印三个成员变量

        System.out.println(brand+"  "+price+"  "+color);

    }

   

}

package com.edu_04;

 

public classTest {

    public static void main(String[] args) {

        //创建一个手机对象,使用set方法给对象赋值(无参构造+set方法)

        Phonep = newPhone();

        p.setBrand("华为");

        p.setColor("黑色");

        p.setPrice(2000);

        System.out.println(p.getBrand()+"  "+p.getColor()+"  "+p.getPrice());

       

        System.out.println("-----------------------------");

        //使用有参构造创建对象并且给对象赋值

        Phonep2 = newPhone("苹果", 5000,"土豪金");

        System.out.println(p2.getBrand()+"  "+p2.getColor()+"  "+p2.getPrice());

       

        System.out.println("-----------------------------");

        p2.call();

        p2.show();

       

    }

 

}

package com.edu_05;

/**

 * 练习:       

    学生类:

        成员变量:

            name,age,address

        构造方法:无参,带参

           

        成员方法:getXxx()/setXxx()

                  show(),study()

 *

 */

public classStudent {

   

    //私有化成员变量

    private Stringname;

    private int age;

    private Stringaddress;

   

    //为私有化的成员变量提供set/get方法

    public void setName(String name){

        this.name = name;

    }

    public String getName(){

        returnname;

    }

   

    public void setAge(int age){

        this.age = age;

    }

    public int getAge(){

        returnage;

    }

   

    public void setAddress(Stringaddress){

        this.address = address;

    }

    public String getAddress(){

        returnaddress;

    }

   

    //有参数和无参数的构造方法

    public Student(){}

    public Student(Stringname,String address,int age){

        this.name = name;

        this.address = address;

        this.age = age;

    }

   

   

    //写一个show()sutdy()

    public void show(){

        System.out.println(name+"  "+age+"  "+address);

    }

    public void study(){

        System.out.println("好好学习天天向上");

    }

   

   

}

package com.edu_05;

 

public classTest {

    public static void main(String[] args) {

        //使用无参构造+set方法创建对象并且给成员变量赋值

        Students = newStudent();

        s.setName("郭德纲");

        s.setAddress("北京");

        s.setAge(45);

        System.out.println(s.getName()+"  "+s.getAge()+"  "+s.getAddress());

       

        System.out.println("---------------------------------");

        //使用有参构造创建对象

        Students2 = newStudent("林志玲","台湾", 42);

        System.out.println(s2.getName()+"  "+s2.getAge()+"  "+s2.getAddress());

       

        System.out.println("-------------------------");

        s2.show();

        s2.study();

       

    }

 

}

package com.edu_06;

/**

 * 七:

    面向对象练习:

    7.1定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。

    add(inta,int b)

    sub(inta,int b)           

    mul(inta,int b)           

    div(inta,int b)

 *

 */

public classMyMath {

   

    //加法

    public int add(int a,int b){

        return a+b;

    }

   

    //减法

    public int sub(int a,int b){

        return a-b;

    }

   

    //乘法

    public int mul(int a,int b){

        return a*b;

    }

   

    //除法

    public int div(int a,int b){

        return a/b;

    }

 

}

package com.edu_06;

 

public classMyMathTest {

    public static void main(String[] args) {

        //创建MyMath对象

        MyMathmy = newMyMath();

       

        //调用这个对象的加减乘除的功能

        System.out.println(my.add(10,20));

        System.out.println(my.sub(20,10));

        System.out.println(my.mul(10, 20));

        System.out.println(my.div(20,10));

       

    }

 

}

package com.edu_07;

/**

 * 定义一个长方形类,定义求周长和面积的方法,然后定义一个测试了Test2,进行测试。

    周长:2*(+)

    面积:长*

 */

public classRectangle {

   

    //提供一个求周长的方法

    public int zhouChang(int width,int height){

        return 2*(width+height);

    }

   

    //求面积的方法

    public int getArear(int width,int height){

        return width*height;

    }

   

 

}

package com.edu_07;

 

public classTest {

    public static void main(String[] args) {

        //创建对象

        Rectangler = newRectangle();

        int zhouChang =r.zhouChang(10, 20);

        System.out.println(zhouChang);

        int arear = r.getArear(10,20);

        System.out.println(arear);

    }

 

}

package com.edu_08;

/**

 * 八:

static关键字

8.1

为了体现共用的数据,java就提供了一个关键字:static

static:

    作用:可以修饰成员变量和成员方法

 

    特点:

        A:随着类的加载而加载

        B:优先于对象存在

        C:被类的所有对象共享

            也是我们判断该不该使用静态修饰一个数据的依据。

            举例:

                饮水机:static

                水杯:特有的内容。

        D:可以通过类名调用

            静态变量:类变量

            非静态变量:实例变量,对象变量

 

            非静态的:创建对象访问

            静态的:可以通过类名,也可以通过对象访问。

 *

 */

public classStudent {

    Stringname;

    int age;

    static StringclassName = "java001";//静态的成员变量,是被所有对象所共享的东西

   

   

    //创建几个构造方法

    public Student(){}

   

    public Student(String name,int age){

        this.name = name;

        this.age = age;

    }

   

    public Student(String name,int age,String className){

        this.name = name;

        this.age = age;

        this.className= className;

    }

   

    //创建一个静态的成员方法

    public static void show(){

        System.out.println("student的静态show方法");

    }

   

 

}

package com.edu_08;

 

public classTest {

    public static void main(String[] args) {

        //使用无参数的构造方法创建一个对象

        Students = newStudent();

        s.name = "庾澄庆";

        s.age = 50;

        System.out.println(s.name+"  "+s.age+"  "+s.className);//庾澄庆  50 java001

       

        System.out.println("-----------------------------");

        //使用带有两个参数的构造方法创建两个对象

        Students2 = newStudent("周杰伦", 37);

        Students3 = newStudent("那英", 50);

        System.out.println(s2.name+"  "+s2.age+"  "+s2.className);//周杰伦  37 java001

        System.out.println(s3.name+"  "+s3.age+"  "+s3.className);//那英  50 java001

       

        System.out.println("-------------------------------");

        //使用三个参数的构造方法创建一个对象

        Students4 = newStudent("汪老师", 50,"java002");

        System.out.println(s4.name+"  "+s4.age+"  "+s4.className);//汪老师  50 java002

        System.out.println(s2.name+"  "+s2.age+"  "+s2.className);//周杰伦  37 java002

        System.out.println(s3.name+"  "+s3.age+"  "+s3.className);//那英  50 java002

       

        System.out.println("---------------------------------");

        //通过类名直接调用静态的成员变量和静态的成员方法

        System.out.println(Student.className);

        Student.show();

       

    }

 

}

package com.edu_09;

/**

 * 8.2(写一个静态成员变量和静态成员方法进行演示)

    static关键字注意事项

        A:在静态方法中是没有this关键字的

            原因:静态的内容随着类的加载而加载,this是随着对象的创建而存在,所以,static中不能有this

        B:静态方法只能访问静态的成员变量和静态的成员方法

            静态方法只能访问静态的成员。

 */

class  Demo{

    static int num = 20;

    int num2 = 30;

   

    //创建几个成员方法

    public void show(){

        System.out.println("demo中的普通的show方法");

    }

   

   

    public static void show2(){

        System.out.println("demo的静态的show2方法");

    }

   

    public static void show3(){

        System.out.println("demo的静态的show3方法");

        //show3方法中调用show方法

        //show();//静态的方法中只能调用静态的方法

        show2();

        //System.out.println(num2);//静态的方法中无法访问非静态的成员变量

        System.out.println(num);

        /**

         * 注意:以上只需要大家记住一点

         * 静态只能访问静态

         */

       

    }

   

}

 

 

public classStaticDemo {

    public static void main(String[] args) {

        //创建Demo对象,调用他的静态的show3()

        Demod = newDemo();

        d.show3();

       

        System.out.println("--------------------");

        Demo.show3();

       

    }

 

}

package com.edu_10;

/**

 * (2)构造方法:

       public String():无参构造方法

        publicString(byte[] bytes):把字节数组转换为字符串

        publicString(char[] value):把字符数组转换为字符串

        publicString(char[] value,int offset,int count):把字符数组的一部分转换为字符串

        publicString(String original):把一个字符串转换为字符串

 *

 */

public classStringDemo {

    public static void main(String[] args) {

        /*//public String():无参构造方法

        Strings = new String();//创建一个一个空字符串:""

        s= "hello";//引用的指向本质上已经发生了变化

        System.out.println(s);*/

       

       

        System.out.println("-------------------");

        /*//public String(byte[] bytes):把字节数组转换为字符串

        Strings = "hello";

        byte[]bys = s.getBytes();

        Strings2 = new String(bys);

        System.out.println(s2);*/

       

        System.out.println("--------------------");

        //public String(char[] value):把字符数组转换为字符串

        /*char[] chs = {'a','b','c','d'};

        Strings = new String(chs);

        System.out.println(s);*/

       

        System.out.println("--------------------");

        //public String(char[] value,int offset,intcount):把字符数组的一部分转换为字符串

        /*char[] chs = {'a','b','c','d'};

        Strings = new String(chs, 2,2);//offect:开始的索引  count:从开始索引开始向后获取多少个字符

        System.out.println(s);*/

       

       

        System.out.println("--------------------");

        //public String(String original):把一个字符串转换为字符串

        Strings = newString("hello");

        System.out.println(s);

       

        /**

         * (需要利用到的一个成员方法)成员方法:

               public intlength():返回此字符串的长度

         */

       

        System.out.println(s.length());

       

    }

 

}

package com.edu_10;

/**

 *      A

        String类的数据特点:

       字符串是常量;它们的值在创建之后不能更改

        面试题:根据以上结论请问输出的s的值是多少

        Strings = "hello";

        s+= "world";

        System.out.println(s);

       

        注意:字符串被创建之后是不能被改变的,但是他的引用的指向可以发生改变

       

 */

public classStringDemo2 {

    public static void main(String[] args) {

        Strings = "hello";

        s+= "world";

        System.out.println(s);

    }

 

}

package com.edu_10;

 

public classStringDemo3 {

    public static void main(String[] args) {

            Strings1 = newString("hello");

            Strings2 = newString("hello");

            System.out.println(s1==s2);//false

            System.out.println(s1.equals(s2));//true

 

            Strings3 = newString("hello");

            Strings4 = "hello";

            System.out.println(s3==s4);//false

            System.out.println(s3.equals(s4));//true

 

            Strings5 = "hello";

            Strings6 = "hello";

            System.out.println(s5==s6);//true

            System.out.println(s5.equals(s6));//true

    }

 

}

package com.edu_10;

 

public classStringDemo4 {

    public static void main(String[] args) {

        Strings1 = "hello";

        Strings2 = "world";

        Strings3 = "helloworld";

        System.out.println(s3 == s1 + s2);//fasle

        System.out.println(s3.equals(s1 +s2));//true

 

        System.out.println(s3 =="hello"+ "world");//true

        System.out.println(s3.equals("hello"+"world"));//true

        /**

         * 变量相加和常量相加的区别?

         * 变量相加:先在内存中开辟空间,再去做加法

         * 常量相加:先加,找是否有这样的数据空间,如果没有才开空间。

         */

    }

 

}

package com.edu_10;

/**

    A:判断功能

      boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)

      boolean equalsIgnoreCase(Stringstr):比较两个字符串的内容是否相同,忽略大小写。(验证码)

      boolean contains(String str):判断字符串中是否包含一个子串。

      boolean startsWith(String str):判断是否以指定的字符串开头

      boolean endsWith(String str):判断是否以指定的字符串结尾

      boolean isEmpty():判断字符串的内容是否为空

      问题:内容为空和对象为空是一个意思吗?

      答:不是

 *

 */

public classStringDemo5 {

    public static void main(String[] args) {

        // boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)

        Strings = "hello";

        Strings2 = "Hello";

        System.out.println(s.equals(s2));

        // boolean equalsIgnoreCase(Stringstr):比较两个字符串的内容是否相同,忽略大小写。(验证码)

        System.out.println(s.equalsIgnoreCase(s2));

       

        // boolean contains(String str):判断字符串中是否包含一个子串。

        System.out.println(s.contains("hel"));

       

        // boolean startsWith(String str):判断是否以指定的字符串开头

        System.out.println(s.startsWith("h"));

       

        // boolean endsWith(String str):判断是否以指定的字符串结尾

        System.out.println(s.endsWith("lo"));

       

        //  booleanisEmpty():判断字符串的内容是否为空

        //String s3 = null;

        Strings3 = "";

        /**

         * java.lang.NullPointerException:空指针异常

         *

         * 对象为空:null,说明这个引用没有指向

         * 字符串为空:就是一个空字符串,字符串中什么也没有

         */

        System.out.println(s3.isEmpty());

       

    }

 

}

package com.edu_10;

/**

    B:获取功能

      String类的获取功能:

      int length():返回字符串的长度。其实就是字符的个数。

      char charAt(int index):返回字符串中指定索引处的字符。

      int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。

            明明说的是字符,为什么是int?

            原因是int类型还可以接收char类型。

            97,'a'是一样的效果。

            但是如果参数是char类型,你就不能写97了。

      int indexOf(String str):返回指定的字符串在字符串中第一次出现的索引。

      String substring(int start):截取从start开始到末尾的字符串。

      String substring(int start,intend):截取从start开始到end结束的字符串。

 *

 */

public classStringDemo6 {

    public static void main(String[] args) {

        // int length():返回字符串的长度。其实就是字符的个数。

        Strings = "hello";

        System.out.println(s.length());

       

        // char charAt(int index):返回字符串中指定索引处的字符。

        System.out.println(s.charAt(0));

       

        //int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。

        System.out.println(s.indexOf('l'));

       

        //String substring(int start):截取从start开始到末尾的字符串。

        Strings2 = s.substring(2);

        System.out.println(s2);

       

        //String substring(int start,int end):截取从start开始到end结束的字符串。

        System.out.println(s.substring(0,3));

        //这里的开始和结束的索引值,全部都是包前不包后

    }

 

}

package com.edu_10;

/**

    C:转换功能

      byte[] getBytes():把字符串转换为字节数组

      char[] toCharArray():把字符串转换为字符数组

      static String valueOf(char[] chs):把字符数组转换为字符串

      static String valueOf(int i):int类型的数据转换为字符串

            valueOf():可以把任意类型的数据转换为字符串。

      String toLowerCase():把字符串转成小写

      String toUpperCase():把字符串转成大写

      String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。

 

 *

    D:其他功能

      A:替换功能

            Stringreplace(char old,char new)

            Stringreplace(String old,String new)

      B:去除字符串两端空格

            Stringtrim()

 *

 */

public classStringDemo7 {

    public static void main(String[] args) {

        // byte[] getBytes():把字符串转换为字节数组

        Strings = "java";

        byte[]bys =s.getBytes();

       

        //char[] toCharArray():把字符串转换为字符数组

//      char[]chs = s.toCharArray();

//      for(int i = 0; i < chs.length; i++) {

//          System.out.println(chs[i]);

//      }

       

       

        //static String valueOf(char[]chs):把字符数组转换为字符串

        char[] chs = {'a','v','c','d'};

        Strings3 = String.valueOf(chs);

        System.out.println(s3);

       

        Strings4 = String.valueOf(12.34);//"12.34"

        System.out.println(s4);

       

        //String toLowerCase():把字符串转成小写

        Strings5 = "HELLoWoErl";

        System.out.println(s5.toLowerCase());

       

        //String toUpperCase():把字符串转成大写

        System.out.println(s5.toUpperCase());

       

        // String concat(Stringstr):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。

        Strings6 = "hello";

        Strings7 = "world";

        System.out.println(s6.concat(s7));

       

        //String replace(char old,char new)

        System.out.println(s6.replace('e','a'));

       

        //String replace(String old,String new)

        System.out.println(s6.replace("hel","hal"));

       

        //String trim()

        Strings8 = "  fdv  gdf  gds   ";

        System.out.println(s8);

        System.out.println(s8.trim());

 

       

    }

 

}

package com.edu_10;

 

public classStringDemo8 {

    public static void main(String[] args) {

        //做一个练习

        //"HElloWorld"

        Strings = "HElloWorld" ;

        Strings2 = s.substring(0, 5);

        Strings3 = s.substring(5);

       

        //转换大小写带拼接

        Strings4 = s2.toUpperCase().concat(s3.toLowerCase());

        System.out.println(s4);

    }

 

}

package com.edu_11;

/**

 * 构造方法:

 *      StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16个字符。

 *      StringBuffer(intcapacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。

 *      StringBuffer(Stringstr):构造一个其中带字符的字符串缓冲区,其初始容量为???个字符。

 *

 * 成员方法:

 *      publicint length():返回长度(字符数)。实际值

 *      publicint capacity():返回当前容量。理论值

 *

 */

public classStringBufferDemo {

    public static void main(String[] args) {

        //StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16个字符。

        StringBuffersb = newStringBuffer();

        System.out.println(sb.length());//实际容量

        System.out.println(sb.capacity());//理论容量

       

        System.out.println("-----------------------");

        //StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。

        StringBuffersb2 = newStringBuffer(10);

        System.out.println(sb2.length());

        System.out.println(sb2.capacity());

       

        System.out.println("---------------------");

        //StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为???个字符。

        StringBuffersb3 = newStringBuffer("hello");

        System.out.println(sb3);

    }

 

}

package com.edu_11;

/**

 *  添加功能:添加元素,并返回本身的对象。

 *      A:publicStringBuffer append(Stringstr):追加数据,在末尾添加

 *      B:publicStringBuffer insert(int offset,Stringstr):插入数据,在指定位置添加   

 *

 */

public classStringBufferDemo2 {

    public static void main(String[] args) {

        //A:public StringBuffer append(Stringstr):追加数据,在末尾添加

        StringBuffersb  =new StringBuffer();

//      sb.append("hello");

//      sb.append(100);

//      sb.append(true);

//      sb.append('a');

//      System.out.println(sb);

       

        sb.append(true).append("hello").append("java").append(100);

        System.out.println(sb);

       

        //B:public StringBuffer insert(int offset,Stringstr):插入数据,在指定位置添加 

        //sb.insert(0, "false");

        sb.insert(4,"fasle");

        System.out.println(sb);

       

    }

 

}

package com.edu_11;

/**

 *  删除功能:

 *      publicStringBuffer deleteCharAt(int index):删除指定索引处的字符

 *      publicStringBuffer delete(int start,int end):删除从start开始到end结束的数据,包左不包右

 */

public classStringBufferDemo3 {

    public static void main(String[] args) {

        //public StringBuffer deleteCharAt(int index):删除指定索引处的字符

        StringBuffersb  =new StringBuffer("hello");

        //sb.deleteCharAt(1);

        //System.out.println(sb);

       

       

        //public StringBuffer delete(int start,intend):

        //删除从start开始到end结束的数据,包左不包右

        sb.delete(1,3);

        System.out.println(sb);

       

    }

 

}

package com.edu_11;

/**

 * 替换功能:

 *      publicStringBuffer replace(int start,int end,Stringstr):str替换从startend的数据

 *

 * 反转功能:

 *      publicStringBuffer reverse()

 * 截取功能:返回值类型是String类型,本身没有发生改变

 *      publicString substring(int start)

 *      publicString substring(int start,int end)

 *

 */

public classStringBufferDemo4 {

    public static void main(String[] args) {

        //替换功能:public StringBuffer replace(int start,int end,Stringstr):

        //str替换从startend的数据

/*      StringBuffersb = new StringBuffer("hello");

        sb.replace(1,3, "ab");

        System.out.println(sb);*/

       

        System.out.println("-----------------------");

        //反转功能:public StringBuffer reverse()

        /*StringBuffer sb = newStringBuffer("hello");

        sb.reverse();

        System.out.println(sb);*/

       

        System.out.println("-------------------");

        //public String substring(int start)

        StringBuffersb = newStringBuffer("hello");

/*      Strings = sb.substring(2);

        System.out.println(s);*/

       

       

        //public String substring(int start,intend)

        Strings = sb.substring(1, 4);

        System.out.println(s);

    }

 

}

package com.edu_11;

/**

 * String   --  StringBuffer

 *      Strings = "hello";

        //方式1

        StringBuffersb1 = new StringBuffer(s);

        //方式2

        StringBuffersb2 = new StringBuffer();

        sb2.append(s);

 *

 */

public classStringBufferDemo5 {

    public static void main(String[] args) {

        //String    --  StringBuffer

        Strings = "hello";

        // 方式1

        StringBuffersb1 = newStringBuffer(s);

        // 方式2

        StringBuffersb2 = newStringBuffer();

        sb2.append(s);

       

        System.out.println("------------------------");

        //StringBuffer -->>String的转换

        //方式1

        //substring(0)截取方法

       

        //方式2String(StringBuffer buffer)

       

        //方式3:调用StringBuffer中的toString()方法

               

    }

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


0 0
原创粉丝点击