从通用模板类型Object调用实际类方法

时间:2016-10-08 17:29:57

标签: java oop data-structures linked-list generic-programming

//程序的主要部分我创建了一个书籍对象并将其插入到通用链接列表中但我无法访问linklist类中的所有书籍方法

Collection {#198 ▼
  #items: array:2 [▼
    0 => Post {#194 ▼
      #fillable: array:6 [▼
        0 => "post_title"
        1 => "post_content"
        2 => "post_author"
        3 => "post_type"
        4 => "created_at"
        5 => "updated_at"
      ]
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:7 [▼
        "id" => 1
        "post_title" => "TEST"
        "post_content" => "Test post"
        "post_author" => 1
        "post_type" => "blog"
        "created_at" => "2016-10-08 14:20:07"
        "updated_at" => "2016-10-08 14:20:07"
      ]
      #original: array:7 [▼
        "id" => 1
        "post_title" => "TEST"
        "post_content" => "Test post"
        "post_author" => 1
        "post_type" => "blog"
        "created_at" => "2016-10-08 14:20:07"
        "updated_at" => "2016-10-08 14:20:07"
      ]
      #relations: array:1 [▼
        "user" => User {#199 ▼
          #fillable: array:4 [▼
            0 => "name"
            1 => "email"
            2 => "password"
            3 => "profile_picture"
          ]
          #hidden: array:2 [▼
            0 => "password"
            1 => "remember_token"
          ]
          #connection: null
          #table: null
          #primaryKey: "id"
          #keyType: "int"
          #perPage: 15
          +incrementing: true
          +timestamps: true
          #attributes: array:9 [▼
            "id" => 1
            "name" => "Sup3rL3on"
            "email" => "codeoncaffeine1@gmail.com"
            "password" => "$2y$10$b6pMtiKt0LcDCeRtTlVJzOL3BvD6Ru1TihbOhM7FOHUscW0daIwGC"
            "profile_picture" => "default-profile-picture"
            "account_type" => 1
            "remember_token" => null
            "created_at" => null
            "updated_at" => null
          ]
          #original: array:9 [▼
            "id" => 1
            "name" => "Sup3rL3on"
            "email" => "codeoncaffeine1@gmail.com"
            "password" => "$2y$10$b6pMtiKt0LcDCeRtTlVJzOL3BvD6Ru1TihbOhM7FOHUscW0daIwGC"
            "profile_picture" => "default-profile-picture"
            "account_type" => 1
            "remember_token" => null
            "created_at" => null
            "updated_at" => null
          ]
          #relations: []
          #visible: []
          #appends: []
          #guarded: array:1 [▼
            0 => "*"
          ]
          #dates: []
          #dateFormat: null
          #casts: []
          #touches: []
          #observables: []
          #with: []
          #morphClass: null
          +exists: true
          +wasRecentlyCreated: false
        }
      ]
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▼
        0 => "*"
      ]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Post {#195 ▼
      #fillable: array:6 [▼
        0 => "post_title"
        1 => "post_content"
        2 => "post_author"
        3 => "post_type"
        4 => "created_at"
        5 => "updated_at"
      ]
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:7 [▼
        "id" => 6
        "post_title" => "TEST"
        "post_content" => "Test post"
        "post_author" => 1
        "post_type" => "blog"
        "created_at" => "2016-10-08 14:20:07"
        "updated_at" => "2016-10-08 14:20:07"
      ]
      #original: array:7 [▼
        "id" => 6
        "post_title" => "TEST"
        "post_content" => "Test post"
        "post_author" => 1
        "post_type" => "blog"
        "created_at" => "2016-10-08 14:20:07"
        "updated_at" => "2016-10-08 14:20:07"
      ]
      #relations: array:1 [▼
        "user" => null
      ]
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▼
        0 => "*"
      ]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

//链接列表类

@extends('layouts.front-header')

@section('content')
<style>
    .hero-image {
        background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url(images/hero-2.jpg);
    }
</style>


<div class="hero-image hero-image-inner-page">
    <div class="hero-image-inner">
        <div class="text">
            <h1>Blog</h1>
        </div>
    </div>
</div>



<main>
    <section class="blog">
        <div class="container">
        <div class="posts">
        <div class="items">
            <!-- 
            <div class="post item">
                <h3 data-field="post_title"></h3>
                <p data-field="post_content"></p>
                <img src="http://fiddle-earth.com/updates/madsa/img/image00.png" alt="">
                <div class="post-data">
                    <img data-field="author_profile_picture" alt="">
                    <p>posted by <strong data-field="post_author"></strong> on <span data-field="post_date"></span></p>
                </div>
            </div> -->


            @foreach ($blog_posts as $blog_post)
                <div class="post item">
                    <h3 data-field="post_title">{{ $blog_post->post_title }}</h3>
                    <p data-field="post_content">{{ $blog_post->post_content }}</p>
                    <!-- <img src="http://fiddle-earth.com/updates/madsa/img/image00.png" alt=""> -->
                    <div class="post-data">
                        <img data-field="author_profile_picture" alt="">
                        @if($blog_post->user()!=null)
                            {{ $blog_post->user->name }}
                        @endif
                        <p>posted by <strong data-field="post_author"></strong> on <span data-field="post_date"></span></p>
                    </div>
                </div>
            @endforeach

        </div>
        <!-- <a href="#" class="items-load">Load more</a> -->
        </div>
        </div>
    </section>
</main>




<script src="assets/js/loadmore.js"></script>
<script>
$('.posts').loadmore({
    source: 'assets/js/json/blog-json.php',
    img: 'uploads/',
    step: 4
});
</script>
@endsection

//想要在显示列表方法

中获取书籍的实际名称和作者名称
 public static void main(String[] args) {
 LinkedList<Book> list;
    list = new LinkedList<>();
   Book a=new Book();
   a.setAuthur("ahsan");
   a.setName("DSA");
   a.setNoOfPages(12);


   list.insertFirst(a);

}

}

1 个答案:

答案 0 :(得分:0)

简短回答:你不能。

答案很长:当你有一个Node<AnyType>时,我们所知道的是该值是AnyType的某个实例。它可能是Book,也可能不是。由于这是我们在编译时无法知道的,我们不能允许调用Book而不是AnyType的方法。

如果您的要求是将书籍从列表中删除,则应将其设为Node<Book>