zoukankan      html  css  js  c++  java
  • 【JAVA】调用类中的属性

    class person
    {
        String name;
        int age;
        String like;
        
        void setName(String name)
        {
            this.name = name;
        }
        
        void setAge(int age)
        {
            this.age =age;
        }
        
        void setLike(String like)
        {
            this.like=like;
        }
        
        void talk()
        {
            System.out.println( "我是:" + name + ",今年:" + age + "岁," + "喜欢:" + like );
        }
    
    }
    
    public class classwork
    {
        public static void main( String[] args ) 
        {
            person p = new person();
            p.setName( "小明");
            p.setAge(20);
            p.setLike("音乐");
            
            p.talk();
        }
    }
    class Book  
    {   
        String title ;  
        double price ;  
        public void printInfo()  // 书籍信息
        {  
            System.out.println("书的名字:" + title + ",价格:" + price) ;
        }
    }
    
    
    public class work2
    {
        public static void main(String args[]) {
            Book bookA = new Book() ;   
            Book bookB = new Book() ;
            bookA.title = "程序设计" ; 
            bookA.price = 40.7 ;       
            bookB.title = "C语言" ;
            bookB.price = 33.9 ;
            bookB = bookA ;            
            bookA.printInfo() ;
            bookB.printInfo();
            
        }
    }
    class Books 
    {
        private String title; 
        private double price ;
        String pub = "天天精彩出版社" ;    
        public Books(String title,double price) 
        {
            this.title = title ;
            this.price = price ;
        }  
        public String getInfo() 
        {
            return "图书名称:" + this.title + ",价格:" + this.price + "元,出版社:" + this.pub ;
        }
    }
    public class work3
    {
        public static void main(String args[]) 
        {
            Books b1 = new Books("Java开发实战经典",59.8) ;
            Books b2 = new Books("Java WEB开发实战经典(基础篇)",49.9) ;
            Books b3 = new Books("Android开发实战经典",68) ;
            System.out.println(b1.getInfo()) ;
            System.out.println(b2.getInfo()) ;
            System.out.println(b3.getInfo()) ;
          
            System.out.println("----------------------出版社改名之后-------------------------") ;
            b1.pub = "每日精彩出版社" ;
            b2.pub = "每日精彩出版社" ;
            b3.pub = "每日精彩出版社" ;
            System.out.println(b1.getInfo()) ;
            System.out.println(b2.getInfo()) ;
            System.out.println(b3.getInfo()) ;
        }
    }
  • 相关阅读:
    shell 网络状态查询 ping curl telnet
    shell 命令 rz sz
    shell 命令之 jps
    Python 之 threading
    根据 MySQL 状态优化 ---- 4. 临时表
    根据 MySQL 状态优化 ---- 3. key_buffer_size
    根据 MySQL 状态优化 ---- 2. 连接数
    根据 MySQL 状态优化 ---- 1. 慢查询
    Linux 服务器的网络配置
    Linux 服务器的网络配置
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/4887999.html
Copyright © 2011-2022 走看看