静态类型是否意味着如果要更改变量的类型就必须强制转换变量?
在 Java 和 C++ 等静态类型语言中,除了“强制转换”之外,还有其他方法可以更改变量的类型吗?
我试图找出动态类型和静态类型在实践中的主要区别是什么,并不断寻找非常学术的定义。 我想知道这对于我的代码来说意味着什么。
Are there any other ways of changing a variable's type in a statically typed language like Java and C++, except 'casting'?
I'm trying to figure out what the main difference is in practical terms between dynamic and static typing and keep finding very academic definitions. I'm wondering what it means in terms of what my code looks like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
确保您不会将静态类型与动态类型与强类型与弱类型混淆。
这两个对立面可以自由组合:
一般来说,静态类型意味着很多错误会被捕获编译器是动态类型语言中的运行时错误 - 但这也意味着您花费大量时间担心类型,在许多情况下是不必要的(请参阅接口与鸭子类型)。
强类型意味着类型之间的任何转换都必须是显式的,无论是通过强制转换还是通过使用转换方法(例如将字符串解析为整数)。 这意味着更多的打字工作,但具有让您能够控制事物的优点,而当系统执行一些模糊的隐式转换时,弱打字通常会导致混乱,这会给您带来完全错误的变量值,从而导致严重破坏十个方法调用线。
Make sure you don't get static vs. dynamic typing confused with strong vs. weak typing.
These two opposites can be combined freely:
Genrally, static typing means that a lot of errors are caught by the compiler which are runtime errors in a dynamically typed language - but it also means that you spend a lot of time worrying about types, in many cases unnecessarily (see interfaces vs. duck typing).
Strong typing means that any conversion between types must be explicit, either through a cast or through the use of conversion methods (e.g. parsing a string into an integer). This means more typing work, but has the advantage of keeping you in control of things, whereas weak typing often results in confusion when the system does some obscure implicit conversion that leaves you with a completely wrong variable value that causes havoc ten method calls down the line.
在 C++/Java 中,您无法更改变量的类型。
静态类型:变量在编译类型时分配一种类型并且不会改变。
动态类型:变量的类型可以在运行时更改,例如在 JavaScript 中:
In C++/Java you can't change the type of a variable.
Static typing: A variable has one type assigned at compile type and that does not change.
Dynamic typing: A variable's type can change while runtime, e.g. in JavaScript:
主要区别在于,在动态类型语言中,直到在运行时使用某个方法时,您才知道该方法是否存在。 在静态类型语言中,检查是在编译时进行的,如果方法不存在,则编译失败。
The main difference is that in dynamically typed languages you don't know until you go to use a method at runtime whether that method exists. In statically typed languages the check is made at compile time and the compilation fails if the method doesn't exist.
类型系统不一定会对代码的外观产生任何影响,例如具有静态类型、类型推断和隐式转换的语言(例如 Scala)看起来很像动态类型语言。 另请参阅:讨论类型系统之前需要了解的内容。
The type system does not necessarily have any impact on what code looks like, e.g. languages with static typing, type inference and implicit conversion (like Scala for instance) look a lot like dynamically typed languages. See also: What To Know Before Debating Type Systems.
您不需要显式转换。 在许多情况下,隐式转换有效。
例如:
但是,您不能更改变量的类型。 不过,您可以在 C++ 中使用联合来实现某种动态类型。
You don't need explicit casting. In many cases implicit casting works.
For example:
You cannot, however, change the type of a variable. You can use unions in C++, though, to achieve some sort of dynamic typing.
让我们看看静态类型语言 Java 和动态语言 JavaScript。 在Java中,对于对象来说,变量是对对象的引用。 对象具有运行时类型,引用也具有类型。 引用的类型必须是运行时对象或其祖先之一的类型。 这就是多态性的工作原理。 您必须进行强制转换才能向上引用类型的层次结构,但不能向下。 编译器确保满足这些条件。 在像 JavaScript 这样的语言中,变量就是变量。 你可以让它指向任何你想要的对象,并且在你检查之前你不知道它的类型。
不过,对于转换,Java 中有很多方法(例如 toInteger 和 toFloat)来进行转换并生成具有相同相对值的新类型的对象。 JavaScript 中也有转换方法,但它们也会生成新对象。
Lets look at Java for he staitically typed language and JavaScript for the dynamc. In Java, for objects, the variable is a reference to an object. The object has a runtime type and the reference has a type. The type of the reference must be the type of the runtime object or one of its ancestors. This is how polymorphism works. You have to cast to go up the hierarchy of the reference type, but not down. The compiler ensures that these conditions are met. In a language like JavaScript, your variable is just that, a variable. You can have it point to whatever object you want, and you don't know the type of it until you check.
For conversions, though, there are lots of methods like toInteger and toFloat in Java to do a conversion and generate an object of a new type with the same relative value. In JavaScript there are also conversion methods, but they generate new objects too.
无论您是否使用静态类型语言,您的代码实际上看起来应该没有太大不同。 仅仅因为您可以用动态类型语言更改变量的数据类型,并不意味着这样做是个好主意。
例如,在 VBScript 中,匈牙利表示法通常用于指定变量的首选数据类型。 这样您就可以轻松发现代码是否混合类型。 (这不是匈牙利表示法的最初用途,但它非常有用。)
通过保持相同的数据类型,您可以避免难以分辨代码实际用途的情况,以及代码根本无法正常工作的情况。 例如:
使用匈牙利表示法,您可以发现混合类型的代码,例如:
Your code should actally not look very much different, regardless if you are using a staticly typed language or not. Just because you can change the data type of a variable in a dynamically typed language, doesn't mean that it is a good idea to do so.
In VBScript, for example, hungarian notation is often used to specify the preferred data type of a variable. That way you can easily spot if the code is mixing types. (This was not the original use of hungarian notation, but it's pretty useful.)
By keeping to the same data type, you avoid situations where it's hard to tell what the code actually does, and situations where the code simply doesn't work properly. For example:
Using hungarian notation you can spot code that is mixing types, like: