笔记6

来源:互联网 发布:阿里云建网站视频 编辑:程序博客网 时间:2024/06/02 12:52
class A{    String s1="";    String s2;}public class H{       public static void main(String[] args)    {        A hh=new A();        printnb(hh.s1);        printnb(hh.s2);    }}

输出:
null

class Tree{    int height;    Tree()    {        print("Planting a seeding");        height=0;    }    Tree(int He)    {        height=He;        print("Creating new Tree that is "+height+" feet tall");    }    void info()    {        print("Tree is "+height+" feet tall");    }    void info(String s)    {        print(s+": Tree is "+height+" feet tall");    }}public class H{       public static void main(String[] args)    {        for(int i=0;i<5;i++)        {            Tree t=new Tree(i);            t.info();            t.info("overloaded method");        }        new Tree();    }}

输出:
Creating new Tree that is 0 feet tall
Tree is 0 feet tall
overloaded method: Tree is 0 feet tall
Creating new Tree that is 1 feet tall
Tree is 1 feet tall
overloaded method: Tree is 1 feet tall
Creating new Tree that is 2 feet tall
Tree is 2 feet tall
overloaded method: Tree is 2 feet tall
Creating new Tree that is 3 feet tall
Tree is 3 feet tall
overloaded method: Tree is 3 feet tall
Creating new Tree that is 4 feet tall
Tree is 4 feet tall
overloaded method: Tree is 4 feet tall
Planting a seeding

public class H{       static void f(String s,int i)    {        print("String: "+s+", int: "+i);    }    static void f(int i,String s)    {        print("int: "+i+", String: "+s);    }    public static void main(String[] args)    {        f("String first",11);        f(99,"Int first");    }

输出:
String: String first, int: 11
int: 99, String: Int first
注:函数名相同,参数顺序不同可以重载但这种方式不常用…会造成混乱

package net.mindview;import static net.mindview.util.Print.*;import static net.mindview.util.Range.*;import java.util.*;public class H{       void f1(char x) {printnb("f1(char) ");}    void f1(byte x) {printnb("f1(byte) ");}    void f1(short x) {printnb("f1(short) ");}    void f1(int x) {printnb("f1(int) ");}    void f1(long x) {printnb("f1(long) ");}    void f1(float x) {printnb("f1(float) ");}    void f1(double x) {printnb("f1(double) ");}    void f2(byte x) {printnb("f1(byte) ");}    void f2(short x) {printnb("f1(short) ");}    void f2(int x) {printnb("f1(int) ");}    void f2(long x) {printnb("f1(long) ");}    void f2(float x) {printnb("f1(float) ");}    void f2(double x) {printnb("f1(double) ");}    void f3(short x) {printnb("f1(short) ");}    void f3(int x) {printnb("f1(int) ");}    void f3(long x) {printnb("f1(long) ");}    void f3(float x) {printnb("f1(float) ");}    void f3(double x) {printnb("f1(double) ");}    void f4(int x) {printnb("f1(int) ");}    void f4(long x) {printnb("f1(long) ");}    void f4(float x) {printnb("f1(float) ");}    void f4(double x) {printnb("f1(double) ");}    void f5(long x) {printnb("f1(long) ");}    void f5(float x) {printnb("f1(float) ");}    void f5(double x) {printnb("f1(double) ");}    void f6(float x) {printnb("f1(float) ");}    void f6(double x) {printnb("f1(double) ");}    void f7(double x) {printnb("f1(double) ");}    void testConstVal()    {        printnb("5: ");        f1(5); f2(5); f3(5); f4(5); f5(5); f6(5); f7(5); print();    }    void testChar()    {        char x='x';        printnb("char: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    void testByte()    {        byte x=0;        printnb("byte: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    void testShort()    {        short x=0;        printnb("short: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    void testInt()    {        int x=0;        printnb("int: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    void testLong()    {        long x=0;        printnb("long: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    void testFloat()    {        float x=0;        printnb("float: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    void testDouble()    {        double x=0;        printnb("double: ");        f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); print();    }    public static void main(String[] args)    {        H p=new H();        p.testConstVal();        p.testChar();        p.testByte();        p.testShort();        p.testInt();        p.testLong();        p.testFloat();        p.testDouble();    }}

输出:
5: f1(int) f1(int) f1(int) f1(int) f1(long) f1(float) f1(double)
char: f1(char) f1(int) f1(int) f1(int) f1(long) f1(float) f1(double)
byte: f1(byte) f1(byte) f1(short) f1(int) f1(long) f1(float) f1(double)
short: f1(short) f1(short) f1(short) f1(int) f1(long) f1(float) f1(double)
int: f1(int) f1(int) f1(int) f1(int) f1(long) f1(float) f1(double)
long: f1(long) f1(long) f1(long) f1(long) f1(long) f1(float) f1(double)
float: f1(float) f1(float) f1(float) f1(float) f1(float) f1(float) f1(double)
double: f1(double) f1(double) f1(double) f1(double) f1(double) f1(double) f1(double)

注:顺序
char 2
byte 1
short 2
int 4
long 8
float 4
double 8
注:char型略有不同,如果找不到参数类型为char的函数,则直接将char提升至int

原创粉丝点击