Java面向对象 C语言字符串常量

时间:2024-11-05 22:49:49

1.

(1).

package liujiawei;

public class Phone {
    String brand;
    double price;


    public void call(){
        System.out.println("手机打电话");
    }
    public void play(){
        System.out.println("手机打游戏");
    }
}
public class phonetest {
    public static void main(String[] args) {
        phone p=new phone();
        p.brand="vivo";
        p.price= Double.parseDouble("4999");
        System.out.println(p.brand);
        System.out.println(p.price);
        p.call();
        p.play();
    }
}

(2).

public class girlfriend{
String name;
double high;
String face;
String age;
public void sleep(){
        System.out.println("女朋友在睡觉");
    }
    public void study(){
        System.out.println("女朋友在学习");
    }
}
public static void main(String[] args) {
    girlfriend f = new girlfriend();
    f.name = "fanwenshuo";
    f.high = Double.parseDouble("17.2");
    f.face = "keai";
    f.age="18";
    f.sleep();
    f.study();
    System.out.println(f.face);
    System.out.println(f.name);
    System.out.println(f.high);
    System.out.println();
}

2.

#if 1
#include<stdio.h>
#include<string.h>
int main(void)
{
    printf("%s\n", "abcEFG");//%s输出字符串
    printf("占据的字节数:%d\n", sizeof("abcEFG"));//最后有0的位置  即尺寸
    printf("长度:%d\n",strlen("abcEFG"));//字符串的长度
    printf("占据的字节数:%d\n", sizeof("abc\0EFG"));//7+1
    printf("长度:%d\n", strlen("abc\0EFG"));//长度:遇到第一个\0
    printf("占据的字节数:%d\n", sizeof(""));//1
    printf("长度:%d\n", strlen(""));//由一对双引号括起来0个或多个字符   //0
    return 0;
}
#endif

相关文章