2021-04-27类和对象

本文探讨了类和对象在面向对象编程中的核心概念,通过Student类实例和Application类的演示,展示了如何创建抽象数据类型和具体实例。理解了类与对象的关系有助于提升代码组织和封装能力。

类与对象的关系

类是一种抽象的数据类型,他是对某一类事物的整体描述/定义,但是不能代表某一个具体的事物
对象是抽象概念的具体实例

package oop.demo2;

//学生类
public class Student {
    //属性:字段
    String name;
    int age;

    //方法
    public void study(){
        System.out.println(this.name+"在学习");;
    }
}

package oop.demo2;

//一个项目应该只存在一个main方法
public class Application {
    public static void main(String[]args){
        //类:抽象的,实例化
        //类实例化后会返回自己的对象
        //student对象就是一个Student类的具体实例
        Student xiaoming = new Student();
        Student xiaohong = new Student();

        xiaoming.name = "小明";
        xiaohong.age = 18;

        System.out.println(xiaoming.name);
        System.out.println(xiaohong.age);

    }
}

面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据

### 基于 C# 实现给定 JSON 结构的代码 以下是使用 C# 生成指定 JSON 结构的完整代码示例。此代码利用了 `Newtonsoft.Json` 库来实现对象序列化[^1]。 ```csharp using System; using Newtonsoft.Json; public class Data { public string psn_no { get; set; } public string insutype { get; set; } public string fixmedins_code { get; set; } public string med_type { get; set; } public string begntime { get; set; } public string endtime { get; set; } public string dise_codg { get; set; } public string dise_name { get; set; } public string oprn_oprt_code { get; set; } public string oprn_oprt_name { get; set; } public string matn_type { get; set; } public string birctrl_type { get; set; } } public class Input { public Data data { get; set; } } public class RootObject { public string infno { get; set; } public string msgid { get; set; } public string mdtrtarea_admvs { get; set; } public string insuplc_admdvs { get; set; } public string recer_sys_code { get; set; } public string dev_no { get; set; } public string dev_safe_info { get; set; } public string cainfo { get; set; } public string signtype { get; set; } public string infver { get; set; } public string opter_type { get; set; } public string opter { get; set; } public string opter_name { get; set; } public string inf_time { get; set; } public string fixmedins_code { get; set; } public string fixmedins_name { get; set; } public string sign_no { get; set; } public Input input { get; set; } } public class Program { public static void Main() { // 创建并初始化 Data 对象 var data = new Data { psn_no = "42010000000000000005391875", insutype = "310", fixmedins_code = "H42092301528", med_type = "11", begntime = "2021-05-30", endtime = "20250412162750", dise_codg = "", dise_name = "", oprn_oprt_code = "", oprn_oprt_name = "", matn_type = "1", birctrl_type = "1" }; // 创建并初始化 Input 对象 var input = new Input { data = data }; // 创建并初始化 RootObject 对象 var rootObject = new RootObject { infno = "2001", msgid = "H42092301528202504121627501665", mdtrtarea_admvs = "420923", insuplc_admdvs = "420141", recer_sys_code = "ZLHIS", dev_no = "", dev_safe_info = "", cainfo = "", signtype = "V1.1.68", infver = "1", opter_type = "1", opter = "66", opter_name = "66", inf_time = "2025-04-12 16:27:50", fixmedins_code = "H42092301528", fixmedins_name = "云梦县人民医院", sign_no = "420900G9100002447472", input = input }; // 将对象序列化为 JSON 字符串 string jsonString = JsonConvert.SerializeObject(rootObject, Formatting.Indented); Console.WriteLine(jsonString); } } ``` 上述代码中定义了多个嵌套以匹配目标 JSON 的结构,并通过 `JsonConvert.SerializeObject` 方法将对象序列化为格式化的 JSON 字符串[^1]。 ### 关键点解析 - **定义**:根据 JSON 的嵌套层次,定义了 `Data`、`Input` `RootObject` 等,确保每个字段都能准确映射到目标 JSON 的键名。 - **属性命名**:中的属性名称与 JSON 中的键名保持一致,以便在序列化时自动匹配[^1]。 - **序列化**:使用 `JsonConvert.SerializeObject` 方法将对象转换为格式化的 JSON 字符串,便于阅读调试。 ### 注意事项 - 需要安装 `Newtonsoft.Json` 库,可以通过 NuGet 包管理器进行安装。 - 如果 JSON 结构复杂或存在动态字段,可以考虑使用字典型 `Dictionary<string, object>` 来处理[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值