java基础知识第十七天

2018-11-03  本文已影响0人  牛倩贱

1.补充昨天的程序

十六—4:

(1.)public  class  Student{

        public  void  study( ) {

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

}

}

(2).public  class  Teacher{

        public  void  test(Student  s) {             // 形式参数Student是一个类名

        s.study()

}

}

(3).//调用Teacher的test方法

public  class  Test{

        public  static  void  main(String [] args){

            Teacher t = new Teacher();

            Student s new Student();

            t.test(s);

}

}

十六—5

(1).public  class  Student{

public  void  study( ) {

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

}

}

(2).public  class  Teacher {

        public  Student  getStudent (){

        Student   s = new  Student ( ) ;

        return s;

}

}

(3).要求通过Teacher得到Student对象,然后调用Student方法

        public  class  Test{

                piblic  static  void  main(String[]args){

                   Teacher t = new Teacher( );

                    Studen  s = t.getTeacher();

                    s.study;

}

}

2.API概述及使用步骤

        应用程序编程接口,java   API 指的就是JDK中提供的各种功能的java类。

使用步骤:

        a.打开帮助文档(API)

        b.点击显示,找到索引,看到输入框

        c.需要什么就输入,例如Random

        d.看包

                java.long包下的类在使用的时候是不需要导包的

        e.看类的描述

                Random类是用于生成随机数的类

        f.看构造方法

                Random( );无参构造方法

                Random  r = new  Random();

        g.看成员方法

            public int  nextInt(int  n):产生一个【0-n)范围的随机数

3.String类的概述和构造方法

        String:字符串类

                    由多个字符组成的一串数据,字符串其本质是一个字符数组

构造方法:

    (1).String(String  original):把字符串数据封装成字符串对象

    (2).String (char[ ] value):把字符数组的数据封装成字符串对象

    (3).String (char [ ] value, int  index,  int  count):把字符串数组中的一部分封装成字符串对象

例:

a.   String  s1 = new  String(“hello”);

           system.out.println("s1:"+s1);

b.  String  s2 = new  String ("chs");

        char [ ] chs = {'h','e','l','l','o'}

        system.out.println("s2"+s2;

c.  char[ ] chs = {'h','e','l','l','o'}

        String  s3 = new String (chs,0,chs.length);

            //从第一个字符开始索引到长度

    system.out.println("s3"+s3);

}

}

4.通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象有什么区别。

        String  s1 = new  String("hello");  //通过构造方法

        String s2 =  “hello”   //直接赋值

例:   System.out.println("s1=s2"+(s1=s2));    //false

            说明new出来的和直接赋值的地址值是不一样的

==:

        基本数据类型:比较的是基本数据类型的值是否相等

        引用数据类型:比较的是引用数据类型的地址值是否相同

上一篇 下一篇

猜你喜欢

热点阅读