首页 > 其他分享 >值类型和引用类型的赋值及深拷贝探究

值类型和引用类型的赋值及深拷贝探究

时间:2022-12-22 15:11:07浏览次数:52  
标签:weatherForecast1 weatherForecast TemperatureC forCopy Date 类型 拷贝 赋值

本文主要讲述的是值类型和引用类型的赋值及深拷贝

对值类型和引用类型的定义以及由来不清晰的可以看我之前的随笔,链接如下:

https://www.cnblogs.com/ShawBlack/p/16997772.html

 

  由值类型和引用类型定义得知,值类型变量中存储的是其数据本身,而引用类型中存储的是数据的地址。

 

  所以很明显,在我们使用赋值符号时

 

 

 

 

  对于引用类型,直接赋值,很明显只是把地址赋值给新的引用类型变量了,使得二者指向的是同一对象。对于引用类型变量的多次赋值,其实也只是多次赋值地址而已。

  举个栗子:

  C#:

internal class Program
    {
        static void Main(string[] args)
        {
            WeatherForecast weatherForecast1 = new WeatherForecast() { TemperatureC = 111, Date = Convert.ToDateTime("1111.01.01") };
            WeatherForecast weatherForecast2 = new WeatherForecast() { TemperatureC = 222, Date = Convert.ToDateTime("2222.02.02") };
            //
            WeatherForecast weatherForecast_forCopy = weatherForecast1;  //将weatherForecast1中的地址赋值给weatherForecast_forCopy,使得二者指向同一个对象
            weatherForecast_forCopy.Date = Convert.ToDateTime("3333.03.03");
            weatherForecast_forCopy.TemperatureC = 333;
            Console.WriteLine("weatherForecast1.TemperatureC = " + weatherForecast1.TemperatureC);
            Console.WriteLine("weatherForecast1.Date = " + weatherForecast1.Date.ToLongDateString());
            //
            weatherForecast_forCopy = weatherForecast2;//将weatherForecast2中的地址赋值给weatherForecast_forCopy,使得二者指向同一个对象
            weatherForecast_forCopy.Date = Convert.ToDateTime("4444.04.04");
            weatherForecast_forCopy.TemperatureC = 444;
            Console.WriteLine("weatherForecast1.TemperatureC = " + weatherForecast1.TemperatureC);
            Console.WriteLine("weatherForecast1.Date = " + weatherForecast1.Date.ToLongDateString());
            Console.WriteLine("weatherForecast2.TemperatureC = " + weatherForecast2.TemperatureC);
            Console.WriteLine("weatherForecast2.Date = " + weatherForecast2.Date.ToLongDateString());
        }
    }

    public class WeatherForecast
    {
        public DateTime Date { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string Summary { get; set; }
    }

  结果:

 

 

  C++:

  

 

标签:weatherForecast1,weatherForecast,TemperatureC,forCopy,Date,类型,拷贝,赋值
From: https://www.cnblogs.com/ShawBlack/p/16998770.html

相关文章

  • C++——拷贝构造和运算符重载
    1.拷贝构造函数1.值传递#include<iostream>usingnamespacestd;classdate{public:date(intyear=1,intmonth=1,intday=1)//全缺省构造{_year=year;......
  • 值类型和引用类型详解(C++/C#混讲)
    作者在初学值类型、引用类型时就一头雾水,相信大部分人也是一样的,现在回过头来总结一下。说起值类型、引用类型这件事呀,那就得从头说起...首先,我们可以将程序运行......
  • 整数范围与类型转换
    -2147483647-1== 2147483648U-2147483647-1<-2147483647-2147483647-1< 2147483647(unsigned)-2147483647-1< 2147483647上面四个表达式成立吗?为什么?并用C语......
  • 用c++代码实现golang里面的map数据类型
    因为之前写过一篇golang数据类型分析的文章。包含slice、map、channel等。想写一篇用其它语言实现golang数据类型的代码,于是选中map作为实验对象。笔者之前写过5年的c++,......
  • 递归深拷贝
    //模拟对象letobj={numberParams:1,functionParams:()=>{console.log('昨天基金全是绿的,只有我的眼睛是红的');},objParams:{......
  • 类型转换
    一、int转stringc++11标准增加的全局函数std::to_string(参数)。参数类型可以是:int、long、longlong、unsigned、unsignedlong、unsignedlonglong、float、double、l......
  • JavaSE-day04-基本概念-数据类型&标识符&键盘录入
    Java基本概念本章包含数据类型,标识符以及键盘录入!数据类型数据类型的分类1.基本数据类型Java中基本数据类型分为4类8种。1.整数:byte,short,int,long细节:整数的......
  • 整数范围与类型转换
    -2147483647-1==2147483648U-2147483647-1<-2147483647-2147483647-1<2147483647(unsigned)-2147483647-1<2147483647上面四个表达式成立吗?为什么?并用C语言......
  • 整数范围与类型转换
    -2147483647-1==2147483648U-2147483647-1<-2147483647-2147483647-1<2147483647(unsigned)-2147483647-1<2147483647上面四个表达式成立吗?为什么?并用C语言......
  • 整数范围与类型转换
    代码#defineINT_MAX2147483647#defineINT_MIN(-INT_MAX-1)#include<stdio.h>#include<string.h>intchecktruefalse(inta){if(a){pri......