Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以将子对象指定给基对象,但如果对集合执行相同操作,则不可能_Java_C#_.net_Oop_Inheritance - Fatal编程技术网

Java 可以将子对象指定给基对象,但如果对集合执行相同操作,则不可能

Java 可以将子对象指定给基对象,但如果对集合执行相同操作,则不可能,java,c#,.net,oop,inheritance,Java,C#,.net,Oop,Inheritance,可以将子对象指定给基础 base1 b1 = new child1(); 但是,如果我们对收藏也这样做,那是不可能的,为什么 List<base1> libase = new List<child1>(); List libase=new List(); 任何有此解释的参考资料都非常有用,请分享 public class base1 { } public class child1 : base1

可以将子对象指定给基础

base1 b1 = new child1();
但是,如果我们对收藏也这样做,那是不可能的,为什么

List<base1> libase = new List<child1>();
List libase=new List();
任何有此解释的参考资料都非常有用,请分享

        public class base1
        {

        }
        public class child1 : base1
        {

        }
        static void Main(string[] args)
        {
            base1 b1 = new child1();
            List<base1> libase = new List<child1>();
            Console.Read();   
        }
公共类base1
{
}
公共类child1:base1
{
}
静态void Main(字符串[]参数)
{
base1 b1=新子女1();
List libase=新列表();
Console.Read();
}
在c语言中,您可以使用以下技巧:

List<base1> libase = new List<base1>(new List<child1>());

关于你的问题,请在这里阅读更多内容

Jorn,这不是C#Java@JornVernee谢谢,我找不到任何与this@dahui这个问题也用Java标记,除了解释基本相同之外。
IEnumerable<base1> libase = new List<child1>();
  public interface IEnumerable<out T> : IEnumerable
  {
    IEnumerator<T> GetEnumerator();
  }
public class List<T>
{}