• 从零自学Java-8.创建第一个对象


    1.创建对象;
    2.使用属性描述对象;
    3.确定对象的行为;
    4.合并对象;
    5.从其他对象继承;
    6.转换对象和其他类型的信息。

    程序NewRoot2:计算输入数的算数平方根并输出

     1 package com.jsample;
     2 
     3 public class NewRoot2 {
     4     public static void main(String[] args){
     5         int number = 100;
     6         if (args.length > 0){
     7             number = Integer.parseInt(args[0]);
     8         }
     9         System.out.println("The square root of "
    10                 + number
    11                 + " is "
    12                 + Math.sqrt(number)
    13         );
    14     }
    View Code

    输出:

    The square root of 169 is 13.0

    程序ModemTester:测试下属四个class的功能

     1 package com.jsample;
     2 
     3 public class ModemTester {
     4     public static void main(String[] args){
     5         CableModem surfBoard = new CableModem();
     6         DslModem gateway = new DslModem();
     7         AcousticModem acoustic = new AcousticModem();
     8         surfBoard.speed = 500000;
     9         gateway.speed = 400000;
    10         acoustic.speed = 300;
    11         System.out.println("Trying the cable modem:");
    12         surfBoard.displaySpeed();
    13         surfBoard.connect();
    14         System.out.println("Trying the DSL modem");
    15         gateway.displaySpeed();
    16         gateway.connect();
    17         System.out.println("Trying the acoustic modem");
    18         acoustic.displaySpeed();
    19         acoustic.connect();
    20         System.out.println("Closing all modems");
    21         surfBoard.disconnect();
    22         gateway.disconnect();
    23         acoustic.disconnect();
    24     }
    25 }
    View Code

    下属class:Modem

     1 package com.jsample;
     2 
     3 public class Modem {
     4     int speed;
     5 
     6     public void displaySpeed(){
     7         System.out.println("Speed: " + speed);
     8     }
     9 
    10     public void disconnect(){
    11         System.out.println("Disconnecting to the Internet");
    12     }
    13 }
    View Code

    下属class:CableModem

     1 package com.jsample;
     2 
     3 public class CableModem extends Modem{
     4     String method = "cable connecttion";
     5 
     6     public void connect(){
     7         System.out.println("Connecting to the Internet");
     8         System.out.println("Using a " + method);
     9     }
    10 }
    View Code

    下属class:DslModem

     1 package com.jsample;
     2 
     3 public class DslModem extends Modem{
     4     String method = "DSL phone connection";
     5 
     6     public void connect(){
     7         System.out.println("Connecting to the Internet");
     8         System.out.println("Using a " + method);
     9     }
    10 }
    View Code

    下属class:AcousticModem

     1 package com.jsample;
     2 
     3 public class AcousticModem extends Modem{
     4     String method = "acoustic connection";
     5 
     6     public void connect(){
     7         System.out.println("Connecting to the Internet");
     8         System.out.println("Using a " + method);
     9     }
    10 }
    View Code

    输出:
    Trying the cable modem:
    Speed: 500000
    Connecting to the Internet
    Using a cable connecttion
    Trying the DSL modem
    Speed: 400000
    Connecting to the Internet
    Using a DSL phone connection
    Trying the acoustic modem
    Speed: 300
    Connecting to the Internet
    Using a acoustic connection
    Closing all modems
    Disconnecting to the Internet
    Disconnecting to the Internet
    Disconnecting to the Internet

  • 相关阅读:
    网页连接无法打开可运行
    SET XACT_ABORT
    用Windows Live Writer写CSDN博客的步骤
    ATO,PTO
    什么是贸易顺差?
    ATO/MTO类机械制造业特点以及ERP需求分析(二)
    如何在早期版本的 Office 中打开并保存 Word 2007、Excel 2007 和 PowerPoint 2007 文件
    Alpha和Beta测试简介
    IIS6.0下 Asp网页访问出现500的问题
    合格的程序员
  • 原文地址:https://www.cnblogs.com/redlogic/p/8604278.html
Copyright © 2020-2023  润新知