zoukankan      html  css  js  c++  java
  • 面向对象和静态导入

    整个项目的结构如下:

    Book.java类:

      1 package superdrew.encapsulation;
      2 /**
      3  * @author superdrew
      4  *    Book类   
      5  *    书名,作者,出版社名,价格;方法包括:信息介绍
      6  *  限定介格必须大于10,如果无效进行提示
      7  *    设计构造方法实现对属性赋值
      8  *    信息介绍方法描述图书所有信息
      9  *    编写测试类,测试图书类的对象及相关方法(测试数据信息自定)
     10  */
     11 public class Book {
     12     private String bookName;//书名
     13     private String author;//作者
     14     private String press;//出版社
     15     private double price;//价格
     16     
     17     /**
     18      * 有参的构造方法 
     19      * @param bookName 书名
     20      * @param author   作者
     21      * @param press           出版社
     22      * @param price           价格
     23      */
     24     public Book(String bookName, String author, String press, double price) {
     25         /*this.bookName = bookName;多行注释
     26         this.author = author;
     27         this.press = press;
     28         this.price = price;*/
     29         //this.price = price;
     30         setAuthor(author);
     31         setBookName(bookName);
     32         setPress(press);
     33         setPrice(price);
     34     }
     35 
     36     /**
     37      * 无参构造
     38      */
     39     public Book() {
     40         
     41     }
     42     
     43     /**
     44      * 对书名属性设置值
     45      * @param bookName  书名
     46      */
     47     public void setBookName(String bookName){
     48         this.bookName = bookName;
     49     }
     50     
     51     /**
     52      * @return  返回书名
     53      */
     54     public String getBookName(){
     55         return bookName;
     56     } 
     57     
     58     /**
     59      * 对书的作者属性设置值
     60      * @param author  作者
     61      */
     62     public void setAuthor(String author){
     63         this.author = author;
     64     }
     65     
     66     /**
     67      * @return  作者
     68      */
     69     public String getAuthor(){
     70         return author;
     71     }
     72     
     73     /**
     74      * 对press属性设置值
     75      * @param press  出版社
     76      */
     77     public void setPress(String press){
     78         this.press = press;
     79     }
     80     
     81     /**
     82      * @return 返回当前press的值
     83      */
     84     public String getPress(){
     85         return press;
     86     }
     87     
     88     /**
     89      * 对价格属性设置值
     90      * @param price  价格
     91      */
     92     public void setPrice(double price){
     93         if(price < 10){
     94             System.out.println("书的价格必须大于10块钱");
     95             this.price = 10;
     96         }else{
     97             this.price = price;
     98         }
     99     }
    100     
    101     /**
    102      * 
    103      * @return 返回当前price的值
    104      */
    105     public double getPrice(){
    106         return price;
    107     }
    108     
    109     /**
    110      * 无返回值的 输出书的具体信息
    111      */
    112     public void showInfo(){
    113         System.out.println("书名:"+bookName);
    114         System.out.println("作者:"+author);
    115         System.out.println("出版社:"+press);
    116         System.out.println("价格:"+price+"元");
    117     }
    118 }
    View Code

    Person.java类:

     1 package superdrew.encapsulation;
     2 
     3 /**
     4  *     人类
     5  * @author superdrew
     6  */
     7 public class Person {//不能使用 protected
     8     //private  默认  protected  public
     9     //成员属性    
    10     //成员变量 变成私有的,自己拥有,别人访问不到
    11     //成员变量改成私有的以后,外部类访问不到本类属性,
    12     //那么 提供 public方法 对属性值进行赋值或者取值 set    赋值。。。。。。get   取值
    13     private String name;
    14     private int age;
    15     private boolean marry;
    16     
    17     public void setMarry(boolean marry){
    18         this.marry = marry;
    19     }
    20     
    21     //boolean类型 get方法 使用is开头 
    22     public boolean /*getMarry*/isMarry(){
    23         return marry;
    24     }
    25     
    26     public Person(){
    27         super();
    28     }
    29 
    30     public Person(String name, int age) {
    31         this.name = name;
    32         /*if(age > 130){
    33             this.age  = 18;
    34         }else if(age < 0){ 
    35             this.age  = 1;
    36         }else{
    37             this.age = age;
    38         }*/
    39         //直接调用 给年龄赋值的方法  可以加this  也可以不用加  
    40         setAge(age);
    41     }
    42     
    43     
    44     public void rest(){
    45         System.out.println(name+"休息");
    46     }
    47     
    48     public void introduce(){
    49         System.out.println("name:"+name+" age:"+age);
    50     }
    51     
    52     //对私有的属性进行赋值  name
    53     public void setName(String name){
    54         //name = name ;局部变量 没有任何意义
    55         this.name = name; //this指向当前类的首地址
    56     }
    57     
    58     //对私有的属性进行赋值  age
    59     public void setAge(int age){
    60         if(age > 130){
    61             this.age  = 18;
    62         }else if(age < 0){
    63             this.age  = 1;
    64         }else{
    65             this.age = age;
    66         }
    67     }
    68     
    69     //私有的属性  取值  name
    70     public String getName(){
    71         return name;//返回当前类的私有成员变量   name
    72     }
    73     
    74     //私有的属性  取值  age
    75     public int getAge(){
    76         return age;//返回当前类的私有成员变量   age
    77     }
    78     
    79 }
    View Code

    Test.java类:

     1 package superdrew.encapsulation;
     2 
     3 public class Test {
     4     public static void main(String[] args) {
     5         
     6         //产生一个对象 叫 book
     7         Book book = new Book();
     8         book.setBookName("鹿鼎记");
     9         book.setAuthor("金庸");
    10         book.setPress("人民文学出版社");
    11         book.setPrice(120);
    12         book.showInfo();
    13         
    14         System.out.println("=========================");
    15         //Book book1 = new Book("绝代双娇","古龙","中国长安出版社",55.5);
    16         Book book1 = new Book();
    17         String bookName = book1.getBookName();//绝代双骄
    18         String author = book1.getAuthor();
    19         String press = book1.getPress();
    20         double price = book1.getPrice();
    21         System.out.println("书名:"+bookName);
    22         System.out.println("作者:"+author);
    23         System.out.println("出版社:"+press);
    24         System.out.println("价格:"+price+"元");
    25     }
    26 }
    View Code

    Import.java类:

     1 package superdrew.oop;
     2 import static java.lang.Math.*;
     3 import java.util.Scanner;
     4 import test.Car;
     5 /**
     6  * @author superdrew
     7  *         import 
     8  *         1.为什么要导入?
     9  *             同一个包中需要导入吗?不要
    10  *             不同的包使用类(或者接口 ) 需要导入
    11  *             能不能不导入?我要使用 car类   需要完整的路径  test.Car 
    12  *         
    13  *         2.如何导入 通过 import 关键字
    14  * 
    15  *         3.要注意的:
    16  *             默认是当前包
    17  *             java.lang 包 下的类 (接口)不需要导入 直接使用
    18  *             import java.lang.*  导入该包  下所有的类和接口 
    19  *             lang.invoke,lang.ref, (不会导入子包以下的内容)
    20  *             如果多个包有同名的类 ,能用两个import导入?不能导入两个同名的类的包
    21  *            The import com.sxt.test.Car collides with another import statement
    22  *            非要使用两个不同的包的类/接口
    23  *            其它同名的类 如果要使用,加上全路径
    24  *
    25  *            静态导入  
    26  *            import static  java.lang.Math.*;
    27  *            导入该类下所有静态的成员变量和成员方法
    28  *
    29  *            批量导入  Ctrl+Shift+O(欧)  一次性导入多个需要导入的包
    30  */
    31 public class Import {
    32     public static void main(String[] args) {
    33         Person p1 = new Person();
    34         Car car = new Car();
    35         superdrew.test.Car car1 = new superdrew.test.Car();
    36         
    37         double pi= PI;
    38         double random = random();
    39         double sqrt = sqrt(5);
    40         Scanner sc = new Scanner(System.in);
    41         
    42     }
    43 }
    View Code

    oop下面的Person类:

     1 package superdrew.oop;
     2 
     3 public class Person {
     4     String name;
     5     int age;
     6     
     7     public Person(){
     8         
     9     }
    10     
    11     public Person(String name,int age){
    12         this.name = name;
    13         this.age = age;
    14     }
    15 }
    View Code

    Car.java类:

     1 package superdrew.test;
     2 
     3 public class Car {
     4     String type;
     5     String color;
     6     
     7     public Car(String type, String color) {
     8         this.type = type;
     9         this.color = color;
    10     }
    11     
    12     public Car() {
    13     }
    14     
    15     
    16 }
    View Code

    Test.java类:

     1 package superdrew.test;
     2 import superdrew.encapsulation.Person;
     3 
     4 public class Test {
     5     public static void main(String[] args) {
     6         
     7         Person person = new Person();
     8         person.setName("小君君");
     9         person.setAge(300);
    10         person.introduce();
    11         person.rest();
    12     }
    13 }
    View Code
  • 相关阅读:
    RocketMQ主从搭建
    Spring Cloud Sleuth
    Spring Cloud Eureka
    Nacos配置中心使用
    Nacos注册中心使用
    Nacos快速入门
    Nginx配置SSL
    并发工具类
    关于类的线程安全
    Docker 入门学习笔记(一)
  • 原文地址:https://www.cnblogs.com/superdrew/p/8054005.html
Copyright © 2011-2022 走看看