java学习(4)-java特性以及修饰,构造

    xiaoxiao2021-03-25  109

    Java学习笔记(4)  

    声明:由于学习是使用JDK7,所有关于java学习所提供的代码段在JDK7环境下均经过测试,没有问题,如果环境不同,可能会有报错!!!  

    这篇来说封装以及java一些关键字,会提供大量的代码段:

    面向对象程序就有以下三个特性:

    1)封装性  2)继承性3)多态性

    这篇主要写封装性以及一些相关内容。

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

            封装的好处是能够提高了代码的复用性和提高安全性。在其他类中仅能通过公共访问方式来进行访问。

    下面提供这篇用来实力的代码段:

    public class Student {

    //定义成员变量 String name;  //姓名 int age; //年龄和 }

    这样写出来的java类在其他类可以对其实例化对象成员变量随便进行赋值,即使有错误也能对类中成员变量进行赋值。

    所以为了提高安全性,对类进行封装是很有必要的。

    具体体现在:将不需要对外提供的内容都隐藏起来。

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

    就是使用修饰符关键字private对成员变量和成员方法进行修饰,使成员变量和成员方法只能在本类中访问,在其他类中只能通过公共访问方式进行访问。(private:私有的

    改进之后完整代码:

    public class Student { //定义成员变量 private String name; private int age; //提供set/get方法 public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } //定义成员方法 public void study(){ System.out.println("学生爱学习!"); } public void playGame(){ System.out.println("学生喜欢打游戏!"); } }

    2.this关键字

    局部变量如果有一个变量和成员变量名称一致,那么优先使用的是局部变量。(就近原则)使用方式见上述set方法,this指代调用该方法的对象。

    3.构造方法 3.1构造方法 作用:给对象的数据进行初始化格式特点:方法名和类名相同。 例如上述类的无参构造方法:public void Student() {}构造方法没有返回值类型,也没有返回值。 3.2构造方法注意事项 1)如果你不提供构造方法,系统会给出默认无参构造方法

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

    3)构造方法也是可以重载的

            总的来说,构造方法分为无参和带参的两种。

    代码:

    //提供无参和有参的构造方法 public Student(){ System.out.println("这是一个无参的构造方法"); } public Student(String name,int age){ System.out.println("这是一个两个参数的构造方法"); this.name = name; this.age = age; }

    这里写出了无参和两个参数的构造方法。

    3.3写到这里就有了两种给成员变量赋值的方法: A:无参构造方法+setXxx()方法进行赋值 B:通过带参的构造方法直接进行赋值 4.成员方法:去掉static的方法。 根据返回值类型: 1)void类型2)非void类型 根据参数列表: 1)无参2)带参 5.标准代码的写法: 成员变量: 构造方法:无参,带参 成员方法:getXxx()/setXxx()等等 这里给出一个学生类的标准类写法:

    完整代码:

    public class Student { //定义成员变量 private String name; private int age; private String sex; //提供set/get方法 public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setSex(String sex){ this.sex = sex; } public String getSex(){ return sex; } //提供无参和有参的构造方法 public Student(){ System.out.println("这是一个无参的构造方法"); } public Student(String name,int age,String sex){ System.out.println("这是一个三个参数的构造方法"); this.name = name; this.age = age; this.sex = sex; } //定义成员方法 public void show(){ System.out.println("这是一个show方法"); System.out.println(name+" "+age+" "+sex); } }

    6.static关键字 6.1为了体现共用的数据,java就提供了一个关键字:static。 作用:可以修饰成员变量和成员方法

    特点:1)随着类的加载而加载 2)优先于对象存在3)被类的所有对象共享4)可以通过类名调用

    6.2   静态变量:类变量 非静态变量:实例变量,对象变量 非静态的:创建对象访问 静态的:可以通过类名,也可以通过对象访问。 6.3static关键字注意事项 1)在静态方法中是没有this关键字的 原因:静态的内容随着类的加载而加载,this是随着对象的创建而存在,所以,static中不能有this。 2)静态方法只能访问静态的成员变量和静态的成员方法 静态方法只能访问静态的成员。 7. String类

    7.1是由多个字符组成的一串数据。(字符序列) 其实字符串可以看成是一个字符数组。

    String类的成员方法特别多,这里给出部分成员方法,并提供相应代码

    7.2构造方法:   public String():无参构造方法   public String(byte[] bytes):把字节数组转换为字符串   public String(char[] value):把字符数组转换为字符串   public String(char[] value,int offset,int count):把字符数组的一部分转换为字符串   public String(String original):把一个字符串转换为字符串  可以使用成员方法:public int length():返回此字符串的长度 7.3String的特点 String类的数据特点:字符串的内容不能发生改变,但是字符串引用的指向是可以改变的。 String s = new String("hello")和String s = "hello"的区别 前者创建了1个或者2个对象 后缀创建了0个或者1个对象 结论:变量相加,先开空间,在加。   常量相加,先加,找是否有这样的数据空间,如果没有才开空间。 7.4String类的成员方法 1)判断功能   boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)   boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相同,忽略大小写。(验证码)   boolean contains(String str):判断字符串中是否包含一个子串。   boolean startsWith(String str):判断是否以指定的字符串开头   boolean endsWith(String str):判断是否以指定的字符串结尾   boolean isEmpty():判断字符串的内容是否为空 2)获取功能 String类的获取功能:   int length():返回字符串的长度。其实就是字符的个数。   char charAt(int index):返回字符串中指定索引处的字符。   int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。   int indexOf(String str):返回指定的字符串在字符串中第一次出现的索引。   String substring(int start):截取从start开始到末尾的字符串。   String substring(int start,int end):截取从start开始到end结束的字符串。 3)转换功能

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

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

     static String valueOf(char[] chs):把字符数组转换为字符串   static String valueOf(int i):把int类型的数据转换为字符串 valueOf():可以把任意类型的数据转换为字符串。   String toLowerCase():把字符串转成小写   String toUpperCase():把字符串转成大写   String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。 4)其他功能   替换功能 String replace(char old,char new) String replace(String old,String new) 去除字符串两端空格

    String trim()

    这里给出一个调用多个String成员方法的代码:

    //调用string方法进行对于字符串的大小写转换 public class StringDemo { public static void main(String[] args) { String s= "HEllowOrld!!!";

    //进行字符串截取

    String s1 = s.substring(0, 5); String s2 = s.substring(5);

    //进行大小写转换 String s3 = s1.toLowerCase(); String s4 = s2.toUpperCase();

    //拼接 System.out.println(s3.concat(s4)); } }

    运行结果:

    8.StringBuffer和StringBuilder

    8.1StringBuffer:线程安全的可变字符序列。 String和StringBuffer的区别? 1)String的内容不可变 2)StringBuffer的内容可变 StringBuffer和StringBuilder的区别? 1)StringBuffer 线程安全,效率低 2)StringBuilder 线程不安全,效率高 总结:线程安全:(同步),效率低

      线程不安全:(不同步),效率高

    8.2 构造方法: StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。 StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。 StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为 "16+字符串长度”个字符。

    调用StringBuffer三个构造方法完整代码:

    调用下面的 length():返回长度(字符数)。实际值和public int capacity():返回当前容量。 理论值 两个成员方法,查看长度:

    public static void main(String[] args) { /** * StringBuffer的三种构造方法 */ //StringBuffer():构造一个其中不带字符的字符串缓冲区 //其初始容量为 16 个字符。 StringBuffer sb = new StringBuffer(); System.out.print("实际容量:"); System.out.println(sb.length()); System.out.print("理论容量:"); System.out.println(sb.capacity()); System.out.println("----------------"); //StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区 //其初始容量为 capacity个字符。 StringBuffer sb2 = new StringBuffer(15); System.out.print("实际容量:"); System.out.println(sb2.length()); System.out.print("理论容量:"); System.out.println(sb2.capacity()); System.out.println("----------------"); //StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。 StringBuffer sb3 = new StringBuffer("hello"); System.out.print("实际容量:"); System.out.println(sb3.length()); System.out.print("理论容量:"); System.out.println(sb3.capacity()); //打印sb3内容 System.out.println(sb3); }

    运行结果:

    8.3成员方法: public int length():返回长度(字符数)。实际值 public int capacity():返回当前容量。 理论值  添加功能:添加元素,并返回本身的对象。 public StringBuffer append(String str):追加数据,在末尾添加

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

    运行代码:

        //添加功能:添加元素,并返回本身的对象。 StringBuffer sb = new StringBuffer("hello"); //public StringBuffer append(String str):追加数据,在末尾添加 //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 sb.append("world"); System.out.println("调用方法后:"); System.out.println(sb); System.out.println("--------"); //public StringBuffer insert(int offset,String str):插入数据,在指定位置添加 //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 System.out.println("调用方法后:"); sb.insert(5, "all"); System.out.println(sb); System.out.println("--------");

    运行结果:

    删除功能:

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

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

    代码:

    //删除功能: StringBuffer sb = new StringBuffer("hello"); //public StringBuffer deleteCharAt(int index):删除指定索引处的字符 //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 sb.deleteCharAt(4); //调用方法后 System.out.println("调用方法后:"); System.out.println(sb); System.out.println("--------"); //public StringBuffer delete(int start,int end):删除从start开始到end结束的数据,包左不包右 //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 sb.delete(2, 4); //调用方法后 System.out.println("调用方法后:"); System.out.println(sb); System.out.println("--------");

    运行结果:

    替换功能:

     public StringBuffer replace(int start,int end,String str):用str替换从start到end的数据

    代码:

    //替换功能: StringBuffer sb = new StringBuffer("hello"); //public StringBuffer replace(int start,int end,String str):用str替换从start到end的数据 //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 sb.replace(1, 4, "ELO"); //调用方法后 System.out.println("调用方法后:"); System.out.println(sb); System.out.println("--------");

    运行结果:

    反转功能:

    public StringBuffer reverse()

    代码:

    //反转功能: StringBuffer sb = new StringBuffer("hello"); //public StringBuffer reverse() //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 sb.reverse(); //调用方法后 System.out.println("调用方法后:"); System.out.println(sb); System.out.println("--------");

    运行结果:

    截取功能:返回值类型是String类型,本身没有发生改变 public String substring(int start)

    public String substring(int start,int end) 

    代码:

    //截取功能:返回值类型是String类型,本身没有发生改变 StringBuffer sb = new StringBuffer("hello"); //public String substring(int start) //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 String sb1 = sb.substring(3); //调用方法后 System.out.println("调用方法后:"); System.out.println(sb1); System.out.println("--------"); //public String substring(int start,int end) //先进行打印 System.out.println("先打印一次:"); System.out.println(sb); //调用方法 String sb2 = sb.substring(2, 4); //调用方法后 System.out.println("调用方法后:"); System.out.println(sb2); System.out.println("--------");

    运行结果:

    相互转换: String -->StringBuffer String s = "HelloWorld"; // 方式1 StringBuffer sb = new StringBuffer(s); // 方式2 StringBuffer sb = new StringBuffer(); sb2.append(s); StringBuffer  -- > String StringBuffer sb = new StringBuffer("world"); //方式1 String s1 = sb.substring(0); //方式2 String s2 = sb.toString(); //方式3

    String s3 = new String(sb);

    代码:

    //String -->StringBuffer System.out.println("String转化StringBuffer"); String s = "HelloWorld"; System.out.println("输出原型String:"); System.out.println(s); // 方式1 StringBuffer sb = new StringBuffer(s); System.out.println("第一种方式结果:"); System.out.println(sb); System.out.println("-------"); // 方式2 StringBuffer sb2 = new StringBuffer(); sb2.append(s); System.out.println("第二种方式结果:"); System.out.println(sb); System.out.println("-------"); //StringBuffer  -- > String System.out.println("StringBuffer转化String"); StringBuffer sb3 = new StringBuffer("world"); System.out.println("输出原型StringBuffer:"); System.out.println(sb3); //方式1 String s1 = sb3.substring(0); System.out.println("第一种方式结果:"); System.out.println(s1); System.out.println("-------"); //方式2 String s2 = sb3.toString(); System.out.println("第二种方式结果:"); System.out.println(s2); System.out.println("-------"); //方式3 String s3 = new String(sb3); System.out.println("第三种方式结果:"); System.out.println(s3); System.out.println("-------");

    运行结果:

    ---第四篇完~Ag

    转载请注明原文地址: https://ju.6miu.com/read-19591.html

    最新回复(0)